-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
instanceof type inference broken in an "if -> else if -> else if" chain #11965
Comments
This is correct, expected behavior. TypeScript has a structural type system and all type tests, even |
The last else if clause is reachable, and the code worked just fine with TS 1.8.10. As a side note, a workaround for the original issue is to use an explicit cast and the code will compile with typescript 2.0.6: if (action instanceof Action1) {
console.log(action.propA); // works
} else if (action instanceof ActionX) {
// The presense of this empty clause is essential
} else if (action instanceof Action2) {
const a = action as Action2;
console.log(a.propB); // now it works
} |
It is not reachable from the standpoint of structural assignability. Even though it is technically reachable at runtime, because class ActionX {}
class FooBar {
public handleAction(action: Object): void {
if (action instanceof ActionX) {
return;
}
action // has type never
}
} |
TypeScript Version: "typescript@2.0.6"
Expected behavior:
The code to compile without an error.
Actual behavior:
The code above throws a compile error:
Error TS2339: Property 'propB' does not exist on type 'never'.
A workaround for the issue is to use an explicit cast for the last "else if" clause:
The text was updated successfully, but these errors were encountered: