Skip to content

Commit 45ca9d2

Browse files
committed
Add test case for instance of type guard with interface with prototype property
1 parent c0b26a9 commit 45ca9d2

File tree

3 files changed

+262
-0
lines changed

3 files changed

+262
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//// [typeGuardOfFormInstanceOfOnInterface.ts]
2+
// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
3+
// and C has a property named 'prototype'
4+
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
5+
// it is a subtype of the type of x, or
6+
// - when false, has no effect on the type of x.
7+
8+
interface C1 {
9+
(): C1;
10+
prototype: C1;
11+
p1: string;
12+
}
13+
interface C2 {
14+
(): C2;
15+
prototype: C2;
16+
p2: number;
17+
}
18+
interface D1 extends C1 {
19+
prototype: D1;
20+
p3: number;
21+
}
22+
var str: string;
23+
var num: number;
24+
var strOrNum: string | number;
25+
26+
var c1: C1;
27+
var c2: C2;
28+
var d1: D1;
29+
var c1Orc2: C1 | C2;
30+
str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1
31+
num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2
32+
str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1
33+
num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1
34+
35+
var c2Ord1: C2 | D1;
36+
num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2
37+
num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1
38+
str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1
39+
var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1
40+
41+
//// [typeGuardOfFormInstanceOfOnInterface.js]
42+
// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
43+
// and C has a property named 'prototype'
44+
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
45+
// it is a subtype of the type of x, or
46+
// - when false, has no effect on the type of x.
47+
var str;
48+
var num;
49+
var strOrNum;
50+
var c1;
51+
var c2;
52+
var d1;
53+
var c1Orc2;
54+
str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1
55+
num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2
56+
str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1
57+
num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1
58+
var c2Ord1;
59+
num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2
60+
num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1
61+
str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1
62+
var r2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOfOnInterface.ts ===
2+
// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
3+
// and C has a property named 'prototype'
4+
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
5+
// it is a subtype of the type of x, or
6+
// - when false, has no effect on the type of x.
7+
8+
interface C1 {
9+
>C1 : C1
10+
11+
(): C1;
12+
>C1 : C1
13+
14+
prototype: C1;
15+
>prototype : C1
16+
>C1 : C1
17+
18+
p1: string;
19+
>p1 : string
20+
}
21+
interface C2 {
22+
>C2 : C2
23+
24+
(): C2;
25+
>C2 : C2
26+
27+
prototype: C2;
28+
>prototype : C2
29+
>C2 : C2
30+
31+
p2: number;
32+
>p2 : number
33+
}
34+
interface D1 extends C1 {
35+
>D1 : D1
36+
>C1 : C1
37+
38+
prototype: D1;
39+
>prototype : D1
40+
>D1 : D1
41+
42+
p3: number;
43+
>p3 : number
44+
}
45+
var str: string;
46+
>str : string
47+
48+
var num: number;
49+
>num : number
50+
51+
var strOrNum: string | number;
52+
>strOrNum : string | number
53+
54+
var c1: C1;
55+
>c1 : C1
56+
>C1 : C1
57+
58+
var c2: C2;
59+
>c2 : C2
60+
>C2 : C2
61+
62+
var d1: D1;
63+
>d1 : D1
64+
>D1 : D1
65+
66+
var c1Orc2: C1 | C2;
67+
>c1Orc2 : C1 | C2
68+
>C1 : C1
69+
>C2 : C2
70+
71+
str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1
72+
>str = c1Orc2 instanceof c1 && c1Orc2.p1 : string
73+
>str : string
74+
>c1Orc2 instanceof c1 && c1Orc2.p1 : string
75+
>c1Orc2 instanceof c1 : boolean
76+
>c1Orc2 : C1 | C2
77+
>c1 : C1
78+
>c1Orc2.p1 : string
79+
>c1Orc2 : C1
80+
>p1 : string
81+
82+
num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2
83+
>num = c1Orc2 instanceof c2 && c1Orc2.p2 : number
84+
>num : number
85+
>c1Orc2 instanceof c2 && c1Orc2.p2 : number
86+
>c1Orc2 instanceof c2 : boolean
87+
>c1Orc2 : C1 | C2
88+
>c2 : C2
89+
>c1Orc2.p2 : number
90+
>c1Orc2 : C2
91+
>p2 : number
92+
93+
str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1
94+
>str = c1Orc2 instanceof d1 && c1Orc2.p1 : string
95+
>str : string
96+
>c1Orc2 instanceof d1 && c1Orc2.p1 : string
97+
>c1Orc2 instanceof d1 : boolean
98+
>c1Orc2 : C1 | C2
99+
>d1 : D1
100+
>c1Orc2.p1 : string
101+
>c1Orc2 : D1
102+
>p1 : string
103+
104+
num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1
105+
>num = c1Orc2 instanceof d1 && c1Orc2.p3 : number
106+
>num : number
107+
>c1Orc2 instanceof d1 && c1Orc2.p3 : number
108+
>c1Orc2 instanceof d1 : boolean
109+
>c1Orc2 : C1 | C2
110+
>d1 : D1
111+
>c1Orc2.p3 : number
112+
>c1Orc2 : D1
113+
>p3 : number
114+
115+
var c2Ord1: C2 | D1;
116+
>c2Ord1 : C2 | D1
117+
>C2 : C2
118+
>D1 : D1
119+
120+
num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2
121+
>num = c2Ord1 instanceof c2 && c2Ord1.p2 : number
122+
>num : number
123+
>c2Ord1 instanceof c2 && c2Ord1.p2 : number
124+
>c2Ord1 instanceof c2 : boolean
125+
>c2Ord1 : C2 | D1
126+
>c2 : C2
127+
>c2Ord1.p2 : number
128+
>c2Ord1 : C2
129+
>p2 : number
130+
131+
num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1
132+
>num = c2Ord1 instanceof d1 && c2Ord1.p3 : number
133+
>num : number
134+
>c2Ord1 instanceof d1 && c2Ord1.p3 : number
135+
>c2Ord1 instanceof d1 : boolean
136+
>c2Ord1 : C2 | D1
137+
>d1 : D1
138+
>c2Ord1.p3 : number
139+
>c2Ord1 : D1
140+
>p3 : number
141+
142+
str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1
143+
>str = c2Ord1 instanceof d1 && c2Ord1.p1 : string
144+
>str : string
145+
>c2Ord1 instanceof d1 && c2Ord1.p1 : string
146+
>c2Ord1 instanceof d1 : boolean
147+
>c2Ord1 : C2 | D1
148+
>d1 : D1
149+
>c2Ord1.p1 : string
150+
>c2Ord1 : D1
151+
>p1 : string
152+
153+
var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1
154+
>r2 : C2 | D1
155+
>D1 : D1
156+
>C2 : C2
157+
>c2Ord1 instanceof c1 && c2Ord1 : C2 | D1
158+
>c2Ord1 instanceof c1 : boolean
159+
>c2Ord1 : C2 | D1
160+
>c1 : C1
161+
>c2Ord1 : C2 | D1
162+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
2+
// and C has a property named 'prototype'
3+
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
4+
// it is a subtype of the type of x, or
5+
// - when false, has no effect on the type of x.
6+
7+
interface C1 {
8+
(): C1;
9+
prototype: C1;
10+
p1: string;
11+
}
12+
interface C2 {
13+
(): C2;
14+
prototype: C2;
15+
p2: number;
16+
}
17+
interface D1 extends C1 {
18+
prototype: D1;
19+
p3: number;
20+
}
21+
var str: string;
22+
var num: number;
23+
var strOrNum: string | number;
24+
25+
var c1: C1;
26+
var c2: C2;
27+
var d1: D1;
28+
var c1Orc2: C1 | C2;
29+
str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1
30+
num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2
31+
str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1
32+
num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1
33+
34+
var c2Ord1: C2 | D1;
35+
num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2
36+
num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1
37+
str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1
38+
var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1

0 commit comments

Comments
 (0)