-
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
Typescript does not implicitly infers types when providing function as arguments? #17520
Comments
This is an effect of TypeScript's type argument inference algorithm:
This differs from the unification based type inference implemented by some functional programming languages, but it has the distinct advantage of being able to make partial inferences in incomplete code which is hugely beneficial to statement completion in IDEs. For example, see #15680 (comment) and the thread in #17237. Now, in your example we get it right when there is an arrow function argument because we classify that as contextually sensitive and process it after first making inferences from other arguments. But we can't handle the situation where the arguments don't appear context sensitive (which the simple identifier |
@ahejlsberg Wouldn't lifting generic parameters in argument types work?
This would solve lots of other cases, like |
@simonbuchan Yes, but this type of unification gets exponentially more complicated when types aren't the simple naked type parameters in your example, but more complex constructs such as union and intersection types. For example, see #15016 (comment). |
Following up this chain, I apologise for my "smart" suggestion raising old wounds :) Looks like #16072 gets us nearly there, thanks for that! |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
When providing
identity
function tomap
, it's return type is incorrect(seex2
), while providing anonymous function as a callback leads to a correct output type(seex1
).TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
Code
Expected behavior:
Return type of
map
function, when providingidentity
function should benumber[]
Actual behavior:
Return type is
{}[]
Also, I've realized that
map
function in 2nd example does not infers evenT
type. It's{}
currently, while it's obvious should be anumber
type, as we are providing array of numbers:The second interesting thing is, that type inferring works fine, if
map
function arguments will be reversed:The text was updated successfully, but these errors were encountered: