-
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
Contextual typing does not flow backward through function return types to the function arguments #5256
Comments
More simply: class Entry<T> {
constructor(callback: (v: T) => number) { }
}
var x: Entry<{foo: number}> = new Entry(v => v.foo); |
Let's go even simpler: function foo<T>(x: (n: T) => T): T { return x(undefined); }
var x: string = foo(p => p.substr(2)); (placeholder since I need to write a longer explanation) |
@mhegazy, Sorry, what is the implication of the |
Sorry, I forgot to write the follow-up I alluded to. After walking through this with the spec, I think the rule we would need to support this is that a function call (or constructor call) contextually typed by That said, it's not clear what runtime semantics this rule corresponds to, or that it wouldn't be an undesirable breaking change. |
It would be fantastic if this could be looked into. |
Moving discussion to #11152 which has clearer explanation |
I believe this is fixed by #16072 |
Consider the following case:
We get a compiler error in the callback accessing
v.foo
thatfoo
does not exist on type{}
since{}
is the default type when it is unable to infer the type. However, in this case we know from theWidget
definition thatT
must be typed to{foo:number}
. But, it appears TypeScript is not using this as an inference-site to properly determineT
.The text was updated successfully, but these errors were encountered: