Skip to content

Commit 42b56cf

Browse files
committed
Add a regression test
1 parent 8b43b3d commit 42b56cf

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [narrowTypeByInstanceof.ts]
2+
class Match {
3+
public range(): any {
4+
return undefined;
5+
}
6+
}
7+
8+
class FileMatch {
9+
public resource(): any {
10+
return undefined;
11+
}
12+
}
13+
14+
type FileMatchOrMatch = FileMatch | Match;
15+
16+
17+
let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
18+
19+
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
20+
let a = elementA.resource().path;
21+
let b = elementB.resource().path;
22+
} else if (elementA instanceof Match && elementB instanceof Match) {
23+
let a = elementA.range();
24+
let b = elementB.range();
25+
}
26+
27+
28+
//// [narrowTypeByInstanceof.js]
29+
var Match = (function () {
30+
function Match() {
31+
}
32+
Match.prototype.range = function () {
33+
return undefined;
34+
};
35+
return Match;
36+
})();
37+
var FileMatch = (function () {
38+
function FileMatch() {
39+
}
40+
FileMatch.prototype.resource = function () {
41+
return undefined;
42+
};
43+
return FileMatch;
44+
})();
45+
var elementA, elementB;
46+
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
47+
var a = elementA.resource().path;
48+
var b = elementB.resource().path;
49+
}
50+
else if (elementA instanceof Match && elementB instanceof Match) {
51+
var a = elementA.range();
52+
var b = elementB.range();
53+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
=== tests/cases/compiler/narrowTypeByInstanceof.ts ===
2+
class Match {
3+
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))
4+
5+
public range(): any {
6+
>range : Symbol(range, Decl(narrowTypeByInstanceof.ts, 0, 17))
7+
8+
return undefined;
9+
>undefined : Symbol(undefined)
10+
}
11+
}
12+
13+
class FileMatch {
14+
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
15+
16+
public resource(): any {
17+
>resource : Symbol(resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
18+
19+
return undefined;
20+
>undefined : Symbol(undefined)
21+
}
22+
}
23+
24+
type FileMatchOrMatch = FileMatch | Match;
25+
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
26+
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
27+
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))
28+
29+
30+
let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
31+
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
32+
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
33+
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
34+
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
35+
36+
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
37+
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
38+
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
39+
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
40+
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
41+
42+
let a = elementA.resource().path;
43+
>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 18, 7))
44+
>elementA.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
45+
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
46+
>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
47+
48+
let b = elementB.resource().path;
49+
>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 19, 7))
50+
>elementB.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
51+
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
52+
>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
53+
54+
} else if (elementA instanceof Match && elementB instanceof Match) {
55+
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
56+
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))
57+
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
58+
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))
59+
60+
let a = elementA.range();
61+
>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 21, 7))
62+
>elementA.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
63+
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
64+
>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
65+
66+
let b = elementB.range();
67+
>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 22, 7))
68+
>elementB.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
69+
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
70+
>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
71+
}
72+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
=== tests/cases/compiler/narrowTypeByInstanceof.ts ===
2+
class Match {
3+
>Match : Match
4+
5+
public range(): any {
6+
>range : () => any
7+
8+
return undefined;
9+
>undefined : undefined
10+
}
11+
}
12+
13+
class FileMatch {
14+
>FileMatch : FileMatch
15+
16+
public resource(): any {
17+
>resource : () => any
18+
19+
return undefined;
20+
>undefined : undefined
21+
}
22+
}
23+
24+
type FileMatchOrMatch = FileMatch | Match;
25+
>FileMatchOrMatch : Match | FileMatch
26+
>FileMatch : FileMatch
27+
>Match : Match
28+
29+
30+
let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
31+
>elementA : Match | FileMatch
32+
>FileMatchOrMatch : Match | FileMatch
33+
>elementB : Match | FileMatch
34+
>FileMatchOrMatch : Match | FileMatch
35+
36+
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
37+
>elementA instanceof FileMatch && elementB instanceof FileMatch : boolean
38+
>elementA instanceof FileMatch : boolean
39+
>elementA : Match | FileMatch
40+
>FileMatch : typeof FileMatch
41+
>elementB instanceof FileMatch : boolean
42+
>elementB : Match | FileMatch
43+
>FileMatch : typeof FileMatch
44+
45+
let a = elementA.resource().path;
46+
>a : any
47+
>elementA.resource().path : any
48+
>elementA.resource() : any
49+
>elementA.resource : () => any
50+
>elementA : FileMatch
51+
>resource : () => any
52+
>path : any
53+
54+
let b = elementB.resource().path;
55+
>b : any
56+
>elementB.resource().path : any
57+
>elementB.resource() : any
58+
>elementB.resource : () => any
59+
>elementB : FileMatch
60+
>resource : () => any
61+
>path : any
62+
63+
} else if (elementA instanceof Match && elementB instanceof Match) {
64+
>elementA instanceof Match && elementB instanceof Match : boolean
65+
>elementA instanceof Match : boolean
66+
>elementA : Match | FileMatch
67+
>Match : typeof Match
68+
>elementB instanceof Match : boolean
69+
>elementB : Match | FileMatch
70+
>Match : typeof Match
71+
72+
let a = elementA.range();
73+
>a : any
74+
>elementA.range() : any
75+
>elementA.range : () => any
76+
>elementA : Match
77+
>range : () => any
78+
79+
let b = elementB.range();
80+
>b : any
81+
>elementB.range() : any
82+
>elementB.range : () => any
83+
>elementB : Match
84+
>range : () => any
85+
}
86+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
 class Match {
2+
public range(): any {
3+
return undefined;
4+
}
5+
}
6+
7+
class FileMatch {
8+
public resource(): any {
9+
return undefined;
10+
}
11+
}
12+
13+
type FileMatchOrMatch = FileMatch | Match;
14+
15+
16+
let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
17+
18+
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
19+
let a = elementA.resource().path;
20+
let b = elementB.resource().path;
21+
} else if (elementA instanceof Match && elementB instanceof Match) {
22+
let a = elementA.range();
23+
let b = elementB.range();
24+
}

0 commit comments

Comments
 (0)