Skip to content

Commit 11377f9

Browse files
committed
Primitive type guards are now order independent
1 parent 7c1b28f commit 11377f9

4 files changed

Lines changed: 275 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//// [typeGuardOfFormTypeOfIsOrderIndependent.ts]
2+
var strOrNum: string | number;
3+
var strOrBool: string | boolean;
4+
var strOrFunc: string | (() => void);
5+
var numOrBool: number | boolean
6+
var str: string;
7+
var num: number;
8+
var bool: boolean;
9+
var func: () => void;
10+
11+
if ("string" === typeof strOrNum) {
12+
// if (typeof strOrNum === "string") {
13+
str = strOrNum;
14+
}
15+
else {
16+
num = strOrNum;
17+
}
18+
if ("function" === typeof strOrFunc) {
19+
func = strOrFunc;
20+
}
21+
else {
22+
str = strOrFunc;
23+
}
24+
if ("number" === typeof numOrBool) {
25+
num = numOrBool;
26+
}
27+
else {
28+
bool = numOrBool;
29+
}
30+
if ("boolean" === typeof strOrBool) {
31+
bool = strOrBool;
32+
}
33+
else {
34+
str = strOrBool;
35+
}
36+
37+
38+
//// [typeGuardOfFormTypeOfIsOrderIndependent.js]
39+
var strOrNum;
40+
var strOrBool;
41+
var strOrFunc;
42+
var numOrBool;
43+
var str;
44+
var num;
45+
var bool;
46+
var func;
47+
if ("string" === typeof strOrNum) {
48+
// if (typeof strOrNum === "string") {
49+
str = strOrNum;
50+
}
51+
else {
52+
num = strOrNum;
53+
}
54+
if ("function" === typeof strOrFunc) {
55+
func = strOrFunc;
56+
}
57+
else {
58+
str = strOrFunc;
59+
}
60+
if ("number" === typeof numOrBool) {
61+
num = numOrBool;
62+
}
63+
else {
64+
bool = numOrBool;
65+
}
66+
if ("boolean" === typeof strOrBool) {
67+
bool = strOrBool;
68+
}
69+
else {
70+
str = strOrBool;
71+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts ===
2+
var strOrNum: string | number;
3+
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
4+
5+
var strOrBool: string | boolean;
6+
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
7+
8+
var strOrFunc: string | (() => void);
9+
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
10+
11+
var numOrBool: number | boolean
12+
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
13+
14+
var str: string;
15+
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
16+
17+
var num: number;
18+
>num : Symbol(num, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 5, 3))
19+
20+
var bool: boolean;
21+
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 6, 3))
22+
23+
var func: () => void;
24+
>func : Symbol(func, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 7, 3))
25+
26+
if ("string" === typeof strOrNum) {
27+
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
28+
29+
// if (typeof strOrNum === "string") {
30+
str = strOrNum;
31+
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
32+
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
33+
}
34+
else {
35+
num = strOrNum;
36+
>num : Symbol(num, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 5, 3))
37+
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
38+
}
39+
if ("function" === typeof strOrFunc) {
40+
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
41+
42+
func = strOrFunc;
43+
>func : Symbol(func, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 7, 3))
44+
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
45+
}
46+
else {
47+
str = strOrFunc;
48+
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
49+
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
50+
}
51+
if ("number" === typeof numOrBool) {
52+
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
53+
54+
num = numOrBool;
55+
>num : Symbol(num, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 5, 3))
56+
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
57+
}
58+
else {
59+
bool = numOrBool;
60+
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 6, 3))
61+
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
62+
}
63+
if ("boolean" === typeof strOrBool) {
64+
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
65+
66+
bool = strOrBool;
67+
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 6, 3))
68+
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
69+
}
70+
else {
71+
str = strOrBool;
72+
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
73+
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
74+
}
75+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts ===
2+
var strOrNum: string | number;
3+
>strOrNum : string | number
4+
5+
var strOrBool: string | boolean;
6+
>strOrBool : string | boolean
7+
8+
var strOrFunc: string | (() => void);
9+
>strOrFunc : string | (() => void)
10+
11+
var numOrBool: number | boolean
12+
>numOrBool : number | boolean
13+
14+
var str: string;
15+
>str : string
16+
17+
var num: number;
18+
>num : number
19+
20+
var bool: boolean;
21+
>bool : boolean
22+
23+
var func: () => void;
24+
>func : () => void
25+
26+
if ("string" === typeof strOrNum) {
27+
>"string" === typeof strOrNum : boolean
28+
>"string" : string
29+
>typeof strOrNum : string
30+
>strOrNum : string | number
31+
32+
// if (typeof strOrNum === "string") {
33+
str = strOrNum;
34+
>str = strOrNum : string
35+
>str : string
36+
>strOrNum : string
37+
}
38+
else {
39+
num = strOrNum;
40+
>num = strOrNum : number
41+
>num : number
42+
>strOrNum : number
43+
}
44+
if ("function" === typeof strOrFunc) {
45+
>"function" === typeof strOrFunc : boolean
46+
>"function" : string
47+
>typeof strOrFunc : string
48+
>strOrFunc : string | (() => void)
49+
50+
func = strOrFunc;
51+
>func = strOrFunc : () => void
52+
>func : () => void
53+
>strOrFunc : () => void
54+
}
55+
else {
56+
str = strOrFunc;
57+
>str = strOrFunc : string
58+
>str : string
59+
>strOrFunc : string
60+
}
61+
if ("number" === typeof numOrBool) {
62+
>"number" === typeof numOrBool : boolean
63+
>"number" : string
64+
>typeof numOrBool : string
65+
>numOrBool : number | boolean
66+
67+
num = numOrBool;
68+
>num = numOrBool : number
69+
>num : number
70+
>numOrBool : number
71+
}
72+
else {
73+
bool = numOrBool;
74+
>bool = numOrBool : boolean
75+
>bool : boolean
76+
>numOrBool : boolean
77+
}
78+
if ("boolean" === typeof strOrBool) {
79+
>"boolean" === typeof strOrBool : boolean
80+
>"boolean" : string
81+
>typeof strOrBool : string
82+
>strOrBool : string | boolean
83+
84+
bool = strOrBool;
85+
>bool = strOrBool : boolean
86+
>bool : boolean
87+
>strOrBool : boolean
88+
}
89+
else {
90+
str = strOrBool;
91+
>str = strOrBool : string
92+
>str : string
93+
>strOrBool : string
94+
}
95+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var strOrNum: string | number;
2+
var strOrBool: string | boolean;
3+
var strOrFunc: string | (() => void);
4+
var numOrBool: number | boolean
5+
var str: string;
6+
var num: number;
7+
var bool: boolean;
8+
var func: () => void;
9+
10+
if ("string" === typeof strOrNum) {
11+
// if (typeof strOrNum === "string") {
12+
str = strOrNum;
13+
}
14+
else {
15+
num = strOrNum;
16+
}
17+
if ("function" === typeof strOrFunc) {
18+
func = strOrFunc;
19+
}
20+
else {
21+
str = strOrFunc;
22+
}
23+
if ("number" === typeof numOrBool) {
24+
num = numOrBool;
25+
}
26+
else {
27+
bool = numOrBool;
28+
}
29+
if ("boolean" === typeof strOrBool) {
30+
bool = strOrBool;
31+
}
32+
else {
33+
str = strOrBool;
34+
}

0 commit comments

Comments
 (0)