Skip to content

Type-unsafe usage of Record<string, () => void> #57105

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

Closed
paskozdilar opened this issue Jan 20, 2024 · 2 comments
Closed

Type-unsafe usage of Record<string, () => void> #57105

paskozdilar opened this issue Jan 20, 2024 · 2 comments

Comments

@paskozdilar
Copy link

πŸ”Ž Search Terms

TypeScript Record Function TypeError

πŸ•— Version & Regression Information

5.3.3

⏯ Playground Link

https://www.typescriptlang.org/play?#code/G4QwTgBAZgrgdgYwLYgA4C4ICUCmCD2YAJgDwDOALmAJZwDmANBABQCUEAvAHwTD7VEeHCAG8AvgG4AsAChQkWIk7R4yNAG0A5EXw4yEOPgoQcAD2qVNAXWkzFCNhIgB6ZxAAqAT1Q4AomDBCAEJZIA

πŸ’» Code

var funcmap: Record<string, () => void> = {};
var func = funcmap['does not exist'];
func(); // TypeError!

πŸ™ Actual behavior

TypeScript compiles successfully (even with --strict option).
TypeScript infers the type of func variable to be () => void, even though it can be undefined when the key doesn't exist.

πŸ™‚ Expected behavior

TypeScript doesn't compile successfully, because the code didn't check whether func is undefined or not.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Jan 20, 2024

Duplicates #13778 (see that and various linked issues there).

You're apparently looking for --noUncheckedIndexedAccess and for many people this turns out to be more annoying than helpful. Often the recommendation is to use (()=>void) | undefined instead of enabling this.

@paskozdilar
Copy link
Author

Oh, I missed that one.
Thanks, I think that flag will do.
Cheers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants