-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
FixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
TypeScript Version: "typescript@2.0.6"
// A *self-contained* demonstration of the problem follows...
class Action1 {
public constructor(public propA: string) {}
}
class ActionX {
}
class Action2 {
public constructor(public propB: string) {}
}
class FooBar {
public handleAction(action: Object): void {
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) {
console.log(action.propB); // throws compile error: Error TS2339: Property 'propB' does not exist on type 'never'.
}
}
}
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:
} else if (action instanceof Action2) {
const a = action as Action2;
console.log(a.propB); // now it works
}
thorn0 and chaten
Metadata
Metadata
Assignees
Labels
FixedA PR has been merged for this issueA PR has been merged for this issue