-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
TypeScript Version: 2.7.0-dev.20171110
Code
// two types discriminated by boolean tag
type Foo = {isFoo: true, foo: string};
type Bar = {isFoo?: false, bar: string};
type Union = Foo | Bar;
declare const a: Union;
// works as expected
if (a.isFoo) {
a.foo; // ok
a.bar; // error
} else {
a.foo; // error
a.bar; // ok
}
// doesn't work inside type guard though
type Any = {isFoo?: boolean};
declare function isUnion(t: Any): t is Union;
declare const b: Any;
if (isUnion(b)) {
// b is Union
if (b.isFoo) {
b.foo; // error
b.bar; // error
} else {
b.foo; // error
b.bar; // error
}
// works with newly assigned variable
const c = b;
if (c.isFoo) {
c.foo; // ok
c.bar; // error
} else {
c.foo; // error
c.bar; // ok
}
}Expected behavior:
Should work the same way with narrowed type.
Actual behavior:
Discriminating between the union members doesn't work inside the type guard. strictNullChecks is enabled.
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed