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

Map containing array with strict nullable doesn't work right #58860

Closed
delosford opened this issue Jun 14, 2024 · 4 comments
Closed

Map containing array with strict nullable doesn't work right #58860

delosford opened this issue Jun 14, 2024 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@delosford
Copy link

πŸ”Ž Search Terms

map containing array may be null or undefined

πŸ•— Version & Regression Information

This is the current behavior even in 5.5.1-rc

⏯ Playground Link

No response

πŸ’» Code

function itsaFunction()
{
	let someMap = new Map<(String | null), (Array<String> | null)>();
	const someString = "someString";

	if (someMap.get(someString) !== null && someMap.get(someString) !== undefined)
		console.log(someMap.get(someString)[0]);

}

πŸ™ Actual behavior

Compiler complains that the result of the Map.get operation could be null or undefined

πŸ™‚ Expected behavior

The null and undefined check should be recognized as ensuring that the Map.get operation does not return a null or undefined.

Additional information about the issue

I posted a similar issue recently about multidimensional arrays, but that was solved by 5.5.0 beta/5.5.1-rc. This problem is not solved by them.

@fatcerberus
Copy link

.get(someString) from the perspective of the compiler is just another method call. There’s no way for it to know it returns the same result every time. So you can’t narrow it that way, and I probably wouldn’t write the code this way anyway. Call .get() once, assign the result to a variable, and narrow that.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 14, 2024
@RyanCavanaugh
Copy link
Member

+1 to the above comment. I'm not even sure given the capability, we would do it -Maps are mutable and the key being there on one call isn't a guarantee it's there in another call. FWIW there's another open feature request which I can't find (because the keywords are useless) about using has as a narrowing method for subsequent get calls.

@jakebailey
Copy link
Member

That'd be #13086 and kinda #9619.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants