-
Notifications
You must be signed in to change notification settings - Fork 12.8k
instanceof with class-like variable RHS erroneously narrows "else" #13918
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
This works in nightly. |
I do not think so. |
The Type system in TS is structural. so the checking for you could however, try and trick the compiler by using generic type parameters, these will not be narrowed to |
Right, I understand that due to structural typing My issue is that a value that is (both structurally and nominally) of a class, is getting narrowed away due to If the RHS of
|
The bug is still present in 2.3rc and in |
TypeScript Version: 2.2-rc
Code
Expected behavior:
p
should have typeProp<{}>
in the else block.Actual behavior:
p
has typenever
in the else block.Because
specialSubclass
is a class-like object with instance typeProp
, TypeScript is assuming thatinstanceof
returning false implies the object is not aProp
. This is a bad assumption, because in this kind of codeinstanceof
only returns true for members of some (unknown) subclass.instanceof
should narrow the false case only if the RHS is an actual class reference. If it's a variable of a class-like type, it should only narrow the true case.The text was updated successfully, but these errors were encountered: