Skip to content

Commit 7f02357

Browse files
committed
Merge pull request #7690 from ivogabe/controlFlowTypesTest
Adds tests to control flow types branch
2 parents bf78470 + 4f936c4 commit 7f02357

14 files changed

+388
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let x: string | boolean | number;
2+
let obj: any;
3+
4+
x = "";
5+
x = x.length;
6+
x; // number
7+
8+
x = true;
9+
(x = "", obj).foo = (x = x.length);
10+
x; // number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let x: string | number | boolean;
2+
let cond: boolean;
3+
4+
(x = "") && (x = 0);
5+
x; // string | number
6+
7+
x = "";
8+
cond && (x = 0);
9+
x; // string | number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let x: string | number | boolean;
2+
let cond: boolean;
3+
4+
(x = "") || (x = 0);
5+
x; // string | number
6+
7+
x = "";
8+
cond || (x = 0);
9+
x; // string | number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let x: string | number | boolean;
2+
let cond: boolean;
3+
4+
cond ? x = "" : x = 3;
5+
x; // string | number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
let cond: boolean;
2+
function a() {
3+
let x: string | number;
4+
x = "";
5+
do {
6+
x; // string
7+
} while (cond)
8+
}
9+
function b() {
10+
let x: string | number;
11+
x = "";
12+
do {
13+
x; // string
14+
x = 42;
15+
break;
16+
} while (cond)
17+
}
18+
function c() {
19+
let x: string | number;
20+
x = "";
21+
do {
22+
x; // string
23+
x = undefined;
24+
if (typeof x === "string") continue;
25+
break;
26+
} while (cond)
27+
}
28+
function d() {
29+
let x: string | number;
30+
x = 1000;
31+
do {
32+
x; // number
33+
x = "";
34+
} while (x = x.length)
35+
x; // number
36+
}
37+
function e() {
38+
let x: string | number;
39+
x = "";
40+
do {
41+
x = 42;
42+
} while (cond)
43+
x; // number
44+
}
45+
function f() {
46+
let x: string | number | boolean | RegExp | Function;
47+
x = "";
48+
do {
49+
if (cond) {
50+
x = 42;
51+
break;
52+
}
53+
if (cond) {
54+
x = true;
55+
continue;
56+
}
57+
x = /a/;
58+
} while (cond)
59+
x; // number | boolean | RegExp
60+
}
61+
function g() {
62+
let x: string | number | boolean | RegExp | Function;
63+
x = "";
64+
do {
65+
if (cond) {
66+
x = 42;
67+
break;
68+
}
69+
if (cond) {
70+
x = true;
71+
continue;
72+
}
73+
x = /a/;
74+
} while (true)
75+
x; // number
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let x: string | number | boolean | RegExp | Function;
2+
let obj: any;
3+
let cond: boolean;
4+
5+
x = /a/;
6+
for (let y in obj) {
7+
x = y;
8+
if (cond) {
9+
x = 42;
10+
continue;
11+
}
12+
if (cond) {
13+
x = true;
14+
break;
15+
}
16+
}
17+
x; // RegExp | string | number | boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let obj: number[];
2+
let x: string | number | boolean | RegExp;
3+
4+
function a() {
5+
x = true;
6+
for (x of obj) {
7+
x = x.toExponential();
8+
}
9+
x; // number | boolean
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
let cond: boolean;
2+
function a() {
3+
let x: string | number | boolean;
4+
for (x = ""; cond; x = 5) {
5+
x; // string | number
6+
}
7+
}
8+
function b() {
9+
let x: string | number | boolean;
10+
for (x = 5; cond; x = x.length) {
11+
x; // number
12+
x = "";
13+
}
14+
}
15+
function c() {
16+
let x: string | number | boolean;
17+
for (x = 5; x = x.toExponential(); x = 5) {
18+
x; // string
19+
}
20+
}
21+
function d() {
22+
let x: string | number | boolean;
23+
for (x = ""; typeof x === "string"; x = 5) {
24+
x; // string
25+
}
26+
}
27+
function e() {
28+
let x: string | number | boolean | RegExp;
29+
for (x = "" || 0; typeof x !== "string"; x = "" || true) {
30+
x; // number | boolean
31+
}
32+
}
33+
function f() {
34+
let x: string | number | boolean;
35+
for (; typeof x !== "string";) {
36+
x; // number | boolean
37+
if (typeof x === "number") break;
38+
x = undefined;
39+
}
40+
x; // string | number
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
let x: string | number | boolean | RegExp;
2+
let cond: boolean;
3+
4+
x = /a/;
5+
if (x /* RegExp */, (x = true)) {
6+
x; // boolean
7+
x = "";
8+
}
9+
else {
10+
x; // boolean
11+
x = 42;
12+
}
13+
x; // string | number
14+
15+
function a() {
16+
let x: string | number;
17+
if (cond) {
18+
x = 42;
19+
}
20+
else {
21+
x = "";
22+
return;
23+
}
24+
x; // number
25+
}
26+
function b() {
27+
let x: string | number;
28+
if (cond) {
29+
x = 42;
30+
throw "";
31+
}
32+
else {
33+
x = "";
34+
}
35+
x; // string
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
let cond: boolean;
2+
function a() {
3+
let x: string | number;
4+
x = "";
5+
while (cond) {
6+
x; // string
7+
}
8+
}
9+
function b() {
10+
let x: string | number;
11+
x = "";
12+
while (cond) {
13+
x; // string
14+
x = 42;
15+
break;
16+
}
17+
}
18+
function c() {
19+
let x: string | number;
20+
x = "";
21+
while (cond) {
22+
x; // string
23+
x = undefined;
24+
if (typeof x === "string") continue;
25+
break;
26+
}
27+
}
28+
function d() {
29+
let x: string | number;
30+
x = "";
31+
while (x = x.length) {
32+
x; // number
33+
x = "";
34+
}
35+
}
36+
function e() {
37+
let x: string | number;
38+
x = "";
39+
while (cond) {
40+
x = 42;
41+
}
42+
x; // string | number
43+
}
44+
function f() {
45+
let x: string | number | boolean | RegExp | Function;
46+
x = "";
47+
while (cond) {
48+
if (cond) {
49+
x = 42;
50+
break;
51+
}
52+
if (cond) {
53+
x = true;
54+
continue;
55+
}
56+
x = /a/;
57+
}
58+
x; // string | number | boolean | RegExp
59+
}
60+
function g() {
61+
let x: string | number | boolean | RegExp | Function;
62+
x = "";
63+
while (true) {
64+
if (cond) {
65+
x = 42;
66+
break;
67+
}
68+
if (cond) {
69+
x = true;
70+
continue;
71+
}
72+
x = /a/;
73+
}
74+
x; // number
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
let x: string | number | boolean | RegExp;
2+
3+
x = "";
4+
x; // string
5+
6+
[x] = [true];
7+
x; // boolean
8+
9+
[x = ""] = [1];
10+
x; // string | number
11+
12+
({x} = {x: true});
13+
x; // boolean
14+
15+
({y: x} = {y: 1});
16+
x; // number
17+
18+
({x = ""} = {x: true});
19+
x; // string | boolean
20+
21+
({y: x = /a/} = {y: 1});
22+
x; // number | RegExp
23+
24+
let a: string[];
25+
26+
for (x of a) {
27+
x; // string
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
let cond: boolean;
2+
function a(x: string | number | boolean) {
3+
x = true;
4+
do {
5+
x; // boolean | string
6+
x = undefined;
7+
} while (typeof x === "string")
8+
x; // number | boolean
9+
}
10+
function b(x: string | number | boolean) {
11+
x = true;
12+
do {
13+
x; // boolean | string
14+
if (cond) continue;
15+
x = undefined;
16+
} while (typeof x === "string")
17+
x; // number | boolean
18+
}
19+
function c(x: string | number) {
20+
x = "";
21+
do {
22+
x; // string
23+
if (cond) break;
24+
x = undefined;
25+
} while (typeof x === "string")
26+
x; // string | number
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let cond: boolean;
2+
function a(x: string | number) {
3+
for (x = undefined; typeof x !== "number"; x = undefined) {
4+
x; // string
5+
}
6+
x; // number
7+
}
8+
function b(x: string | number) {
9+
for (x = undefined; typeof x !== "number"; x = undefined) {
10+
x; // string
11+
if (cond) continue;
12+
}
13+
x; // number
14+
}
15+
function c(x: string | number) {
16+
for (x = undefined; typeof x !== "number"; x = undefined) {
17+
x; // string
18+
if (cond) break;
19+
}
20+
x; // string | number
21+
}

0 commit comments

Comments
 (0)