-
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 fails with immediate type guards #18562
Comments
Should wait on #17600 before working on this. |
Since #17600 got merged, can we pick this ticket up again? Please let me know how I could help. Example of the workaround, which does not scale well:
Many of us are just used to writing this like P.S. As always, thank you so much for the wonderful work you're all doing. :) |
@andy-ms can this be picked up again? Filter functions under strictNullChecks are a pain to workaround at this point |
Related: #10734 Note that all examples in the original post are working now. The second comment is about inferring a function to be a type guard without an explicit annotation. E.g., |
The examples in the OP should all now be working with #5101 fixed. The examples in #18562 (comment) require the type guards to be automatically inferred, which is tracked by #16069 |
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: master / 2.6.0-dev20170914 / whatever the playground has currently
I searched for
immediate type guard
andtype guard inference
, I hope I didn't miss an existing issue.The issue is that the type inference fails for overloads with type guards such as the one for Array.filter when the type guard is passed as an immediate value. See also #7657 The problem seems to be that TS infers the type of the argument
e
of the type guard from the type of the callback parameter of filter. In doing so, the return type of type guard is lost. If you specify the argumente
of the type guard explicitly, it works as expected.Code
playground
Expected behavior:
All cases should work.
Actual behavior:
See above.
The text was updated successfully, but these errors were encountered: