File tree Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments