-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Base class is narrowed to never
after User-defined Type Guard on subclass
#57193
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
Your After "correcting" both you get the expected results: TS playground . You have just observed a normal subtype elimination process - in your version you have "removed" |
Thanks for catching that. I did intend to write
I also noticed that adding a new private field to
I think the surprising bit is that this happens when removing In any case, the behavior seems intended. Thank you for the help! |
All type checks involving objects are structural, either directly or indirectly. Even the ones that look nominal, like classes with private members (nominality is emulated by having the source location of the private field be treated as part of the "structure", which can have some surprising effects, e.g. #56146 and especially #55235). Pervasive structural typing is why you get behavior like described here: |
π Search Terms
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ssl=6&ssc=1&pln=7&pc=1#code/MYGwhgzhAECCB2BLAtmE0DeAoa0AOArgEYiLDSoDWApgHID2iE1AFAJSY67QBO1ALgR7xoAIniNmorgF8scrKEgwAIvQDm0agA9+1eABMYCFGk65CJMtAj8w-a0xZgkqEAC44rtG08vT6EzQaprY3LwCQiL+btCI8LYuwNT0AGZeAbLyWFipBPDADvQierbO3h4Zbr42-DzxoVyI6SwhAHROMT4cYdx8gsLQXSBtVHSSrGxZuP1RQxWjYDQMTJNcAPTr4bgAegD82bn5hYjF0KX8AEzlAZ4m1Z629fCNuM3QN7HxiQUp6SE9LgzSKDYaLZYTdjTCIDaILMYrZhQ3CbbbQfbZIA
π» Code
Edit: Switched
animal is Animal
toanimal is Dog
. Caught by @Andarist #57193 (comment).π Actual behavior
Under the
test()
function, theanimal
object is narrowed tonever
.π Expected behavior
The
animal
object is not narrowed kept asAnimal
.Additional information about the issue
This seems to be different when using a user defined type guard vs an inline
instanceof
check which surprised me.The text was updated successfully, but these errors were encountered: