TypeScript/tests/baselines/reference/typeGuardOfFormInstanceOf.types
2015-04-15 16:44:20 -07:00

133 lines
3 KiB
Plaintext

=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOf.ts ===
// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
// and C has a property named 'prototype'
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
// it is a subtype of the type of x, or
// - when false, has no effect on the type of x.
class C1 {
>C1 : C1
p1: string;
>p1 : string
}
class C2 {
>C2 : C2
p2: number;
>p2 : number
}
class D1 extends C1 {
>D1 : D1
>C1 : C1
p3: number;
>p3 : number
}
var str: string;
>str : string
var num: number;
>num : number
var strOrNum: string | number;
>strOrNum : string | number
var c1Orc2: C1 | C2;
>c1Orc2 : C1 | C2
>C1 : C1
>C2 : C2
str = c1Orc2 instanceof C1 && c1Orc2.p1; // C1
>str = c1Orc2 instanceof C1 && c1Orc2.p1 : string
>str : string
>c1Orc2 instanceof C1 && c1Orc2.p1 : string
>c1Orc2 instanceof C1 : boolean
>c1Orc2 : C1 | C2
>C1 : typeof C1
>c1Orc2.p1 : string
>c1Orc2 : C1
>p1 : string
num = c1Orc2 instanceof C2 && c1Orc2.p2; // C2
>num = c1Orc2 instanceof C2 && c1Orc2.p2 : number
>num : number
>c1Orc2 instanceof C2 && c1Orc2.p2 : number
>c1Orc2 instanceof C2 : boolean
>c1Orc2 : C1 | C2
>C2 : typeof C2
>c1Orc2.p2 : number
>c1Orc2 : C2
>p2 : number
str = c1Orc2 instanceof D1 && c1Orc2.p1; // D1
>str = c1Orc2 instanceof D1 && c1Orc2.p1 : string
>str : string
>c1Orc2 instanceof D1 && c1Orc2.p1 : string
>c1Orc2 instanceof D1 : boolean
>c1Orc2 : C1 | C2
>D1 : typeof D1
>c1Orc2.p1 : string
>c1Orc2 : D1
>p1 : string
num = c1Orc2 instanceof D1 && c1Orc2.p3; // D1
>num = c1Orc2 instanceof D1 && c1Orc2.p3 : number
>num : number
>c1Orc2 instanceof D1 && c1Orc2.p3 : number
>c1Orc2 instanceof D1 : boolean
>c1Orc2 : C1 | C2
>D1 : typeof D1
>c1Orc2.p3 : number
>c1Orc2 : D1
>p3 : number
var c2Ord1: C2 | D1;
>c2Ord1 : C2 | D1
>C2 : C2
>D1 : D1
num = c2Ord1 instanceof C2 && c2Ord1.p2; // C2
>num = c2Ord1 instanceof C2 && c2Ord1.p2 : number
>num : number
>c2Ord1 instanceof C2 && c2Ord1.p2 : number
>c2Ord1 instanceof C2 : boolean
>c2Ord1 : C2 | D1
>C2 : typeof C2
>c2Ord1.p2 : number
>c2Ord1 : C2
>p2 : number
num = c2Ord1 instanceof D1 && c2Ord1.p3; // D1
>num = c2Ord1 instanceof D1 && c2Ord1.p3 : number
>num : number
>c2Ord1 instanceof D1 && c2Ord1.p3 : number
>c2Ord1 instanceof D1 : boolean
>c2Ord1 : C2 | D1
>D1 : typeof D1
>c2Ord1.p3 : number
>c2Ord1 : D1
>p3 : number
str = c2Ord1 instanceof D1 && c2Ord1.p1; // D1
>str = c2Ord1 instanceof D1 && c2Ord1.p1 : string
>str : string
>c2Ord1 instanceof D1 && c2Ord1.p1 : string
>c2Ord1 instanceof D1 : boolean
>c2Ord1 : C2 | D1
>D1 : typeof D1
>c2Ord1.p1 : string
>c2Ord1 : D1
>p1 : string
var r2: D1 | C2 = c2Ord1 instanceof C1 && c2Ord1; // C2 | D1
>r2 : C2 | D1
>D1 : D1
>C2 : C2
>c2Ord1 instanceof C1 && c2Ord1 : D1
>c2Ord1 instanceof C1 : boolean
>c2Ord1 : C2 | D1
>C1 : typeof C1
>c2Ord1 : D1