-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type guard not working as expected in function overloading with union types #5826
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
I think this is happening because we try to eliminate all types in the union that are assignable to |
I think this may be a regression - in the playground, there's no errors for this example. |
I think I've figured out why this is a regression - when I swapped narrowing to work from outside in rather than inside out (lol) in #5442 so empty sets could correctly propagate down and compound/nested type guards would work as expected, at the same time I made it so behaviors like this became visible. We had always been narrowing with the logic/spec saying this should happen, but since we'd narrow by the So fixing a set of other bugs exposed this one. :D While the new behavior is a change, the behavior described in the post, as Daniel says, is by design. Since This is actually problematic - So @ahejlsberg, this behavior stays (since it's correct, according to our spec - it was incorrect before) unless we want to add a nominal relationship to use with |
@remojansen as a workaround while we figure this out (or if it just sticks around), you can either brand your classes with dummy members ( |
@weswigham 👍 thanks for that |
The following works as expected:
However, when I use an union type instead of
any
in the implementation signature:I get a compilation error:
Looks like the type guard is not working as expected. I can use an
if
instead ofif else
to fix the error:I'm using TypeScript
1.8.0-dev.20151129
with gulp-typescript:And the following
tsconfig.json
compilation:The source code that I used to discover this issue can be found here.
Thanks!
The text was updated successfully, but these errors were encountered: