-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Function can return wrong values despite type #30603
Comments
I think this is an unsoundness that exists to support index access types on
The base constraint of
The way to prevent the unsoundness would be to write it using two variables, though I'm not sure this actually solves your problem. function get123<K extends keyof Type, T extends Type = Type>(): T[K] {
return 123;
} EDIT: Just to add the caveat that I think this is what it's doing, and why, but nothing I say is official or guaranteed to be correct. |
See this comment by Anders for (much) more detail. |
Feels bad - but does that mean we should mark this as a duplicate @ahejlsberg? |
Would it be too much to restrict the unsoundness to cases where the object type is generic, but the key type is concrete? This example would be the converse and use the sound path. |
@jack-williams I'll give it a try. |
TypeScript Version: 3.4.0-dev.20190326
Search Terms: Function Generic Extends Keyof Property
Code
Expected behavior:
The function
get123
would yield an error as the return type might not be correct as it depends onK
.Actual behavior:
No error for
get123
. That function would obviously always return 123.Playground Link: Example on TypeScript Playground
The text was updated successfully, but these errors were encountered: