Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a map.has(x), then map.get(x) can't be undefined #46328

Closed
DaniBr opened this issue Oct 12, 2021 · 3 comments
Closed

If a map.has(x), then map.get(x) can't be undefined #46328

DaniBr opened this issue Oct 12, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@DaniBr
Copy link

DaniBr commented Oct 12, 2021

Bug Report

πŸ”Ž map has undefined, if map has, map get undefined

πŸ•— v4.4.3 (latest, October 2021)

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const MathNum: Map<string, number> = new Map([
	[ 'Ο€', 3.1415926536 ],
	[ 'e', 2.7182818284 ],
])

let c = 'e'
let n = 0

if (MathNum.has(c)) {
	n = MathNum.get(c) // here is the problem
}

πŸ™ Actual behavior

Yields an error: Type 'number | undefined' is not assignable to type 'number'.

πŸ™‚ Expected behavior

Shouldn't be any problem, because we already checked that the map contains the requested key.

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 12, 2021

Duplicate of #30268. Used search terms: has get in:title

@jcalz
Copy link
Contributor

jcalz commented Oct 12, 2021

Duplicate of #13086, #9619 search terms β€œmap has type guard”

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Oct 12, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants