TypeScript/tests/cases/compiler/flowControlTypeGuardThenSwitch.ts
Wesley Wigham b080aa9440 Fix #16778 - use previous type and not declared type (#17381)
* Fix #16778 - use previous type to check discriminable type and not declared type

* Rename prevType -> computedType
2017-07-26 15:27:02 -07:00

36 lines
549 B
TypeScript

enum Kind {
A,
B,
}
interface Base {
kind: Kind;
}
interface A extends Base {
kind: Kind.A;
yar: any;
}
interface B extends Base {
kind: Kind.B;
gar: any;
}
type Both = A | B;
function isBoth(x: Base): x is Both {
return true;
}
let foo: Base = undefined;
if (isBoth(foo)) {
switch (foo.kind) {
case Kind.A:
const myA: A = foo; // Should not be an error
break;
case Kind.B:
const myB: B = foo;
break;
}
}