Skip to content

Commit 152c77c

Browse files
committed
Add test cases for typeof x == s and typeof x != s form of typeguard which has no effect on narrowing type
1 parent 45ca9d2 commit 152c77c

6 files changed

Lines changed: 372 additions & 0 deletions
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//// [typeGuardOfFormTypeOfEqualEqualHasNoEffect.ts]
2+
class C { private p: string };
3+
4+
var strOrNum: string | number;
5+
var strOrBool: string | boolean;
6+
var numOrBool: number | boolean
7+
var strOrC: string | C;
8+
9+
// typeof x == s has not effect on typeguard
10+
if (typeof strOrNum == "string") {
11+
var r1 = strOrNum; // string | number
12+
}
13+
else {
14+
var r1 = strOrNum; // string | number
15+
}
16+
17+
if (typeof strOrBool == "boolean") {
18+
var r2 = strOrBool; // string | boolean
19+
}
20+
else {
21+
var r2 = strOrBool; // string | boolean
22+
}
23+
24+
if (typeof numOrBool == "number") {
25+
var r3 = numOrBool; // number | boolean
26+
}
27+
else {
28+
var r3 = numOrBool; // number | boolean
29+
}
30+
31+
if (typeof strOrC == "Object") {
32+
var r4 = strOrC; // string | C
33+
}
34+
else {
35+
var r4 = strOrC; // string | C
36+
}
37+
38+
//// [typeGuardOfFormTypeOfEqualEqualHasNoEffect.js]
39+
var C = (function () {
40+
function C() {
41+
}
42+
return C;
43+
})();
44+
;
45+
var strOrNum;
46+
var strOrBool;
47+
var numOrBool;
48+
var strOrC;
49+
// typeof x == s has not effect on typeguard
50+
if (typeof strOrNum == "string") {
51+
var r1 = strOrNum; // string | number
52+
}
53+
else {
54+
var r1 = strOrNum; // string | number
55+
}
56+
if (typeof strOrBool == "boolean") {
57+
var r2 = strOrBool; // string | boolean
58+
}
59+
else {
60+
var r2 = strOrBool; // string | boolean
61+
}
62+
if (typeof numOrBool == "number") {
63+
var r3 = numOrBool; // number | boolean
64+
}
65+
else {
66+
var r3 = numOrBool; // number | boolean
67+
}
68+
if (typeof strOrC == "Object") {
69+
var r4 = strOrC; // string | C
70+
}
71+
else {
72+
var r4 = strOrC; // string | C
73+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfEqualEqualHasNoEffect.ts ===
2+
class C { private p: string };
3+
>C : C
4+
>p : string
5+
6+
var strOrNum: string | number;
7+
>strOrNum : string | number
8+
9+
var strOrBool: string | boolean;
10+
>strOrBool : string | boolean
11+
12+
var numOrBool: number | boolean
13+
>numOrBool : number | boolean
14+
15+
var strOrC: string | C;
16+
>strOrC : string | C
17+
>C : C
18+
19+
// typeof x == s has not effect on typeguard
20+
if (typeof strOrNum == "string") {
21+
>typeof strOrNum == "string" : boolean
22+
>typeof strOrNum : string
23+
>strOrNum : string | number
24+
25+
var r1 = strOrNum; // string | number
26+
>r1 : string | number
27+
>strOrNum : string | number
28+
}
29+
else {
30+
var r1 = strOrNum; // string | number
31+
>r1 : string | number
32+
>strOrNum : string | number
33+
}
34+
35+
if (typeof strOrBool == "boolean") {
36+
>typeof strOrBool == "boolean" : boolean
37+
>typeof strOrBool : string
38+
>strOrBool : string | boolean
39+
40+
var r2 = strOrBool; // string | boolean
41+
>r2 : string | boolean
42+
>strOrBool : string | boolean
43+
}
44+
else {
45+
var r2 = strOrBool; // string | boolean
46+
>r2 : string | boolean
47+
>strOrBool : string | boolean
48+
}
49+
50+
if (typeof numOrBool == "number") {
51+
>typeof numOrBool == "number" : boolean
52+
>typeof numOrBool : string
53+
>numOrBool : number | boolean
54+
55+
var r3 = numOrBool; // number | boolean
56+
>r3 : number | boolean
57+
>numOrBool : number | boolean
58+
}
59+
else {
60+
var r3 = numOrBool; // number | boolean
61+
>r3 : number | boolean
62+
>numOrBool : number | boolean
63+
}
64+
65+
if (typeof strOrC == "Object") {
66+
>typeof strOrC == "Object" : boolean
67+
>typeof strOrC : string
68+
>strOrC : string | C
69+
70+
var r4 = strOrC; // string | C
71+
>r4 : string | C
72+
>strOrC : string | C
73+
}
74+
else {
75+
var r4 = strOrC; // string | C
76+
>r4 : string | C
77+
>strOrC : string | C
78+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//// [typeGuardOfFormTypeOfNotEqualHasNoEffect.ts]
2+
class C { private p: string };
3+
4+
var strOrNum: string | number;
5+
var strOrBool: string | boolean;
6+
var numOrBool: number | boolean
7+
var strOrC: string | C;
8+
9+
// typeof x != s has not effect on typeguard
10+
if (typeof strOrNum != "string") {
11+
var r1 = strOrNum; // string | number
12+
}
13+
else {
14+
var r1 = strOrNum; // string | number
15+
}
16+
17+
if (typeof strOrBool != "boolean") {
18+
var r2 = strOrBool; // string | boolean
19+
}
20+
else {
21+
var r2 = strOrBool; // string | boolean
22+
}
23+
24+
if (typeof numOrBool != "number") {
25+
var r3 = numOrBool; // number | boolean
26+
}
27+
else {
28+
var r3 = numOrBool; // number | boolean
29+
}
30+
31+
if (typeof strOrC != "Object") {
32+
var r4 = strOrC; // string | C
33+
}
34+
else {
35+
var r4 = strOrC; // string | C
36+
}
37+
38+
//// [typeGuardOfFormTypeOfNotEqualHasNoEffect.js]
39+
var C = (function () {
40+
function C() {
41+
}
42+
return C;
43+
})();
44+
;
45+
var strOrNum;
46+
var strOrBool;
47+
var numOrBool;
48+
var strOrC;
49+
// typeof x != s has not effect on typeguard
50+
if (typeof strOrNum != "string") {
51+
var r1 = strOrNum; // string | number
52+
}
53+
else {
54+
var r1 = strOrNum; // string | number
55+
}
56+
if (typeof strOrBool != "boolean") {
57+
var r2 = strOrBool; // string | boolean
58+
}
59+
else {
60+
var r2 = strOrBool; // string | boolean
61+
}
62+
if (typeof numOrBool != "number") {
63+
var r3 = numOrBool; // number | boolean
64+
}
65+
else {
66+
var r3 = numOrBool; // number | boolean
67+
}
68+
if (typeof strOrC != "Object") {
69+
var r4 = strOrC; // string | C
70+
}
71+
else {
72+
var r4 = strOrC; // string | C
73+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNotEqualHasNoEffect.ts ===
2+
class C { private p: string };
3+
>C : C
4+
>p : string
5+
6+
var strOrNum: string | number;
7+
>strOrNum : string | number
8+
9+
var strOrBool: string | boolean;
10+
>strOrBool : string | boolean
11+
12+
var numOrBool: number | boolean
13+
>numOrBool : number | boolean
14+
15+
var strOrC: string | C;
16+
>strOrC : string | C
17+
>C : C
18+
19+
// typeof x != s has not effect on typeguard
20+
if (typeof strOrNum != "string") {
21+
>typeof strOrNum != "string" : boolean
22+
>typeof strOrNum : string
23+
>strOrNum : string | number
24+
25+
var r1 = strOrNum; // string | number
26+
>r1 : string | number
27+
>strOrNum : string | number
28+
}
29+
else {
30+
var r1 = strOrNum; // string | number
31+
>r1 : string | number
32+
>strOrNum : string | number
33+
}
34+
35+
if (typeof strOrBool != "boolean") {
36+
>typeof strOrBool != "boolean" : boolean
37+
>typeof strOrBool : string
38+
>strOrBool : string | boolean
39+
40+
var r2 = strOrBool; // string | boolean
41+
>r2 : string | boolean
42+
>strOrBool : string | boolean
43+
}
44+
else {
45+
var r2 = strOrBool; // string | boolean
46+
>r2 : string | boolean
47+
>strOrBool : string | boolean
48+
}
49+
50+
if (typeof numOrBool != "number") {
51+
>typeof numOrBool != "number" : boolean
52+
>typeof numOrBool : string
53+
>numOrBool : number | boolean
54+
55+
var r3 = numOrBool; // number | boolean
56+
>r3 : number | boolean
57+
>numOrBool : number | boolean
58+
}
59+
else {
60+
var r3 = numOrBool; // number | boolean
61+
>r3 : number | boolean
62+
>numOrBool : number | boolean
63+
}
64+
65+
if (typeof strOrC != "Object") {
66+
>typeof strOrC != "Object" : boolean
67+
>typeof strOrC : string
68+
>strOrC : string | C
69+
70+
var r4 = strOrC; // string | C
71+
>r4 : string | C
72+
>strOrC : string | C
73+
}
74+
else {
75+
var r4 = strOrC; // string | C
76+
>r4 : string | C
77+
>strOrC : string | C
78+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class C { private p: string };
2+
3+
var strOrNum: string | number;
4+
var strOrBool: string | boolean;
5+
var numOrBool: number | boolean
6+
var strOrC: string | C;
7+
8+
// typeof x == s has not effect on typeguard
9+
if (typeof strOrNum == "string") {
10+
var r1 = strOrNum; // string | number
11+
}
12+
else {
13+
var r1 = strOrNum; // string | number
14+
}
15+
16+
if (typeof strOrBool == "boolean") {
17+
var r2 = strOrBool; // string | boolean
18+
}
19+
else {
20+
var r2 = strOrBool; // string | boolean
21+
}
22+
23+
if (typeof numOrBool == "number") {
24+
var r3 = numOrBool; // number | boolean
25+
}
26+
else {
27+
var r3 = numOrBool; // number | boolean
28+
}
29+
30+
if (typeof strOrC == "Object") {
31+
var r4 = strOrC; // string | C
32+
}
33+
else {
34+
var r4 = strOrC; // string | C
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class C { private p: string };
2+
3+
var strOrNum: string | number;
4+
var strOrBool: string | boolean;
5+
var numOrBool: number | boolean
6+
var strOrC: string | C;
7+
8+
// typeof x != s has not effect on typeguard
9+
if (typeof strOrNum != "string") {
10+
var r1 = strOrNum; // string | number
11+
}
12+
else {
13+
var r1 = strOrNum; // string | number
14+
}
15+
16+
if (typeof strOrBool != "boolean") {
17+
var r2 = strOrBool; // string | boolean
18+
}
19+
else {
20+
var r2 = strOrBool; // string | boolean
21+
}
22+
23+
if (typeof numOrBool != "number") {
24+
var r3 = numOrBool; // number | boolean
25+
}
26+
else {
27+
var r3 = numOrBool; // number | boolean
28+
}
29+
30+
if (typeof strOrC != "Object") {
31+
var r4 = strOrC; // string | C
32+
}
33+
else {
34+
var r4 = strOrC; // string | C
35+
}

0 commit comments

Comments
 (0)