-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Inferred type does not work for generic reduce function with when not used as a lambda #27708
Comments
Simplified example: function id<T>(x: T): T { return x; }
function callIt<T, U>(f: (t: T) => U, x: T): U { return f(x); }
const x = callIt(id, 0); // Infers `callIt<{}, {}>` so `x` is `{}`. No error if order of parameters is reversed. |
I think in the past we've called this a design limitation and told people to invert their parameter order; but because of what we've been looking at recently w.r.t. proper unification, I'll mark this as a bug instead and ping @ahejlsberg for input. You think this is worth reconsidering? |
I'd love to reconsider, but it is a hard problem. An inference scheme based on unification would presumably get this example right, but we know from experiments that unification surfaces hard problems for which we don't have an answer. See #24626 and other issues linked in that thread. We could consider tweaks to the existing algorithm to defer inference from arguments that have a generic function type (similar to what we do for function expressions with contextually typed parameters), but that would only work for simple generic function arguments not for structures containing generic functions values. So, not sure that's worthwhile. |
Closing in favor of #9366. |
This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed for automatic house-keeping purposes. |
TypeScript Version: 3.1
Search Terms: type inferrence
Code
Playground
Expected behavior:
Both
reducedOne
andreducedTwo
should be typed asPromise<string>
Actual behavior:
reducedOne
is typed as{}
This issue only occurs when the function has a generic type. This works fine:
Playground
The text was updated successfully, but these errors were encountered: