Skip to content

Commit b93cdec

Browse files
committed
Add regression test
1 parent 2d1639f commit b93cdec

File tree

4 files changed

+256
-0
lines changed

4 files changed

+256
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//// [nestedLoopTypeGuards.ts]
2+
// Repros from #10378
3+
4+
function f1() {
5+
var a: boolean | number | string;
6+
if (typeof a !== 'boolean') {
7+
// a is narrowed to "number | string"
8+
for (var i = 0; i < 1; i++) {
9+
for (var j = 0; j < 1; j++) {}
10+
if (typeof a === 'string') {
11+
// a is narrowed to "string'
12+
for (var j = 0; j < 1; j++) {
13+
a.length; // Should not error here
14+
}
15+
}
16+
}
17+
}
18+
}
19+
20+
function f2() {
21+
var a: string | number;
22+
if (typeof a === 'string') {
23+
while (1) {
24+
while (1) {}
25+
if (typeof a === 'string') {
26+
while (1) {
27+
a.length; // Should not error here
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
//// [nestedLoopTypeGuards.js]
35+
// Repros from #10378
36+
function f1() {
37+
var a;
38+
if (typeof a !== 'boolean') {
39+
// a is narrowed to "number | string"
40+
for (var i = 0; i < 1; i++) {
41+
for (var j = 0; j < 1; j++) { }
42+
if (typeof a === 'string') {
43+
// a is narrowed to "string'
44+
for (var j = 0; j < 1; j++) {
45+
a.length; // Should not error here
46+
}
47+
}
48+
}
49+
}
50+
}
51+
function f2() {
52+
var a;
53+
if (typeof a === 'string') {
54+
while (1) {
55+
while (1) { }
56+
if (typeof a === 'string') {
57+
while (1) {
58+
a.length; // Should not error here
59+
}
60+
}
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
=== tests/cases/compiler/nestedLoopTypeGuards.ts ===
2+
// Repros from #10378
3+
4+
function f1() {
5+
>f1 : Symbol(f1, Decl(nestedLoopTypeGuards.ts, 0, 0))
6+
7+
var a: boolean | number | string;
8+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))
9+
10+
if (typeof a !== 'boolean') {
11+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))
12+
13+
// a is narrowed to "number | string"
14+
for (var i = 0; i < 1; i++) {
15+
>i : Symbol(i, Decl(nestedLoopTypeGuards.ts, 6, 16))
16+
>i : Symbol(i, Decl(nestedLoopTypeGuards.ts, 6, 16))
17+
>i : Symbol(i, Decl(nestedLoopTypeGuards.ts, 6, 16))
18+
19+
for (var j = 0; j < 1; j++) {}
20+
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
21+
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
22+
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
23+
24+
if (typeof a === 'string') {
25+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))
26+
27+
// a is narrowed to "string'
28+
for (var j = 0; j < 1; j++) {
29+
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
30+
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
31+
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
32+
33+
a.length; // Should not error here
34+
>a.length : Symbol(String.length, Decl(lib.d.ts, --, --))
35+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))
36+
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
function f2() {
44+
>f2 : Symbol(f2, Decl(nestedLoopTypeGuards.ts, 16, 1))
45+
46+
var a: string | number;
47+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))
48+
49+
if (typeof a === 'string') {
50+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))
51+
52+
while (1) {
53+
while (1) {}
54+
if (typeof a === 'string') {
55+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))
56+
57+
while (1) {
58+
a.length; // Should not error here
59+
>a.length : Symbol(String.length, Decl(lib.d.ts, --, --))
60+
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))
61+
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
62+
}
63+
}
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
=== tests/cases/compiler/nestedLoopTypeGuards.ts ===
2+
// Repros from #10378
3+
4+
function f1() {
5+
>f1 : () => void
6+
7+
var a: boolean | number | string;
8+
>a : string | number | boolean
9+
10+
if (typeof a !== 'boolean') {
11+
>typeof a !== 'boolean' : boolean
12+
>typeof a : string
13+
>a : string | number | boolean
14+
>'boolean' : "boolean"
15+
16+
// a is narrowed to "number | string"
17+
for (var i = 0; i < 1; i++) {
18+
>i : number
19+
>0 : number
20+
>i < 1 : boolean
21+
>i : number
22+
>1 : number
23+
>i++ : number
24+
>i : number
25+
26+
for (var j = 0; j < 1; j++) {}
27+
>j : number
28+
>0 : number
29+
>j < 1 : boolean
30+
>j : number
31+
>1 : number
32+
>j++ : number
33+
>j : number
34+
35+
if (typeof a === 'string') {
36+
>typeof a === 'string' : boolean
37+
>typeof a : string
38+
>a : string | number
39+
>'string' : "string"
40+
41+
// a is narrowed to "string'
42+
for (var j = 0; j < 1; j++) {
43+
>j : number
44+
>0 : number
45+
>j < 1 : boolean
46+
>j : number
47+
>1 : number
48+
>j++ : number
49+
>j : number
50+
51+
a.length; // Should not error here
52+
>a.length : number
53+
>a : string
54+
>length : number
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
function f2() {
62+
>f2 : () => void
63+
64+
var a: string | number;
65+
>a : string | number
66+
67+
if (typeof a === 'string') {
68+
>typeof a === 'string' : boolean
69+
>typeof a : string
70+
>a : string | number
71+
>'string' : "string"
72+
73+
while (1) {
74+
>1 : number
75+
76+
while (1) {}
77+
>1 : number
78+
79+
if (typeof a === 'string') {
80+
>typeof a === 'string' : boolean
81+
>typeof a : string
82+
>a : string
83+
>'string' : "string"
84+
85+
while (1) {
86+
>1 : number
87+
88+
a.length; // Should not error here
89+
>a.length : number
90+
>a : string
91+
>length : number
92+
}
93+
}
94+
}
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Repros from #10378
2+
3+
function f1() {
4+
var a: boolean | number | string;
5+
if (typeof a !== 'boolean') {
6+
// a is narrowed to "number | string"
7+
for (var i = 0; i < 1; i++) {
8+
for (var j = 0; j < 1; j++) {}
9+
if (typeof a === 'string') {
10+
// a is narrowed to "string'
11+
for (var j = 0; j < 1; j++) {
12+
a.length; // Should not error here
13+
}
14+
}
15+
}
16+
}
17+
}
18+
19+
function f2() {
20+
var a: string | number;
21+
if (typeof a === 'string') {
22+
while (1) {
23+
while (1) {}
24+
if (typeof a === 'string') {
25+
while (1) {
26+
a.length; // Should not error here
27+
}
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)