1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
//// [flowControlTypeGuardThenSwitch.ts]
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;
}
}
//// [flowControlTypeGuardThenSwitch.js]
var Kind;
(function (Kind) {
Kind[Kind["A"] = 0] = "A";
Kind[Kind["B"] = 1] = "B";
})(Kind || (Kind = {}));
function isBoth(x) {
return true;
}
var foo = undefined;
if (isBoth(foo)) {
switch (foo.kind) {
case Kind.A:
var myA = foo; // Should not be an error
break;
case Kind.B:
var myB = foo;
break;
}
}
|