Skip to content

Commit ce5a3f4

Browse files
committed
Add more tests
1 parent a0c5608 commit ce5a3f4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tests/cases/compiler/controlFlowInstanceof.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,23 @@ function foo(x: A | undefined) {
7777
x; // A
7878
}
7979
x; // A
80+
}
81+
82+
// X is neither assignable to Y nor a subtype of Y
83+
// Y is assignable to X, but not a subtype of X
84+
85+
interface X {
86+
x?: string;
87+
}
88+
89+
class Y {
90+
y: string;
91+
}
92+
93+
function goo(x: X) {
94+
x;
95+
if (x instanceof Y) {
96+
x.y;
97+
}
98+
x;
8099
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Repro from #10145
2+
3+
interface A { type: 'A' }
4+
interface B { type: 'B' }
5+
6+
function isA(x: A | B): x is A { return x.type === 'A'; }
7+
function isB(x: A | B): x is B { return x.type === 'B'; }
8+
9+
function foo1(x: A | B): any {
10+
x; // A | B
11+
if (isA(x)) {
12+
return x; // A
13+
}
14+
x; // B
15+
if (isB(x)) {
16+
return x; // B
17+
}
18+
x; // never
19+
}
20+
21+
function foo2(x: A | B): any {
22+
x; // A | B
23+
if (x.type === 'A') {
24+
return x; // A
25+
}
26+
x; // B
27+
if (x.type === 'B') {
28+
return x; // B
29+
}
30+
x; // never
31+
}

0 commit comments

Comments
 (0)