Skip to content

Commit 9277b3f

Browse files
committed
Add test
1 parent 5ebfad6 commit 9277b3f

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Repro from #7271
2+
3+
class C1 { item: string }
4+
class C2 { item: string[] }
5+
class C3 { item: string }
6+
7+
function foo1(x: C1 | C2 | C3): string {
8+
if (x instanceof C1) {
9+
return x.item;
10+
}
11+
else if (x instanceof C2) {
12+
return x.item[0];
13+
}
14+
else if (x instanceof C3) {
15+
return x.item;
16+
}
17+
return "error";
18+
}
19+
20+
function isC1(c: C1 | C2 | C3): c is C1 { return c instanceof C1 }
21+
function isC2(c: C1 | C2 | C3): c is C2 { return c instanceof C2 }
22+
function isC3(c: C1 | C2 | C3): c is C3 { return c instanceof C3 }
23+
24+
function foo2(x: C1 | C2 | C3): string {
25+
if (isC1(x)) {
26+
return x.item;
27+
}
28+
else if (isC2(x)) {
29+
return x.item[0];
30+
}
31+
else if (isC3(x)) {
32+
return x.item;
33+
}
34+
return "error";
35+
}
36+
37+
// More tests
38+
39+
class A { a: string }
40+
class A1 extends A { }
41+
class A2 { a: string }
42+
class B extends A { b: string }
43+
44+
function goo(x: A) {
45+
if (x instanceof A) {
46+
x; // A
47+
}
48+
else {
49+
x; // never
50+
}
51+
if (x instanceof A1) {
52+
x; // A1
53+
}
54+
else {
55+
x; // A
56+
}
57+
if (x instanceof A2) {
58+
x; // A2
59+
}
60+
else {
61+
x; // A
62+
}
63+
if (x instanceof B) {
64+
x; // B
65+
}
66+
else {
67+
x; // A
68+
}
69+
}

0 commit comments

Comments
 (0)