-
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
Type inference algorithm depends on function argument order? (bug?) #17237
Comments
There is no enough information for
See #15680 (comment). |
Thank you. That comment does explain it. And @ahejlsberg is right: turns out that So type "inference working left-to-right for contextually typed arguments" is by design? It might be worthwhile to document this, since it is (at least for me) surprising behaviour that I haven't seen in any other language. Maybe in the Advanced Types section of the handbook? Thank you |
Ideally order would not matter, but we have architectural constraints that prevent a fully-optimal solution. The TL;DR version is that we're not set up to contextually type a function, typecheck its body, undo that work, and then try again with a different contextual type (this could also potentially be very, very slow in certain edge cases) |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.4.1
I have a function that has 3 parameters:
I was surprised to discover that my program suddenly breaks (fails to compile) if I reorder the parameters:
Of course the arguments were swapped at the call site of
f
as well. The failure is due to type inference working differently.Below is a complete example showing the issue.
leftJoin1
andleftJoin2
are identical functions, but with their parameter order swapped.test1
andtest2
callleftJoin1
andleftJoin2
respectively (with the appropriate argument ordering).Notice that the pair of
leftJoin1
+test1
functions works fine, while the pair ofleftJoin2
+test2
functions is broken.To really let this sink in: delete the functions
leftJoin2
andtest2
. Then manually swap the parameters ofleftJoin1
together with swapping the arguments to it intest1
, and observe how the program will have changed from valid to broken.tsconfig.json
The text was updated successfully, but these errors were encountered: