-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic inference different between equivalent function expression and shorthand method in object literal #32230
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
Comments
Nit: These are shorthand method definitions, not function declarations.
This doesn't seem correct. function example<TFoo, TBar, TCallback extends Callback<TFoo, TBar>>(
foo: TFoo,
callback: TCallback,
bar: TBar,
): TCallback {
callback(foo, bar);
return callback;
} In which case the first call will definitely observe |
@ahejlsberg I might be missing something but I don't see any reason for the two to behave differently |
Thanks, I had a hard time finding correct name for that.
Oh yeah, I described it poorly. Changed it to " |
I am trying to investigate it myself. This bug appears after this commit. Why exactly non-fixing mapper causes that I am yet to find. |
This issue is somewhat involved. The core issue is cache invalidation during type inference. Specifically, when we make new inferences for a particular type parameter we only invalidate the cache for that type parameter. In the example above, I think the fix is to invalidate all caches for non-fixed type parameters when inferences are made for a particular type parameter in an inference context. I will look into it. |
TypeScript Version: 3.5.1
Search Terms: parameter, inference, order, generic, function expression, function declaration
Code
Expected behavior:
foo
andbar
should be inferred as number and string respectively in both casesActual behavior:
When not using arrow function expression
bar
is not inferred, even thoughfoo
is infered in both casesPlayground Link: click
Related Issues: This code worked in TS 3.3.3 , so #31814 is most definitely related, and so probably #30215 is the root cause. #6627 is somewhat related.
Comment: Type inference stops working for types which appear in arguments after inner call to a generic function when function declaration appears in callback return. Using function expressions instead is limiting because those does not have correct
this
context.Is this behavior caused by the same limitations as #31814, or is it something what can be improved?
The text was updated successfully, but these errors were encountered: