Skip to content

Commit a3a0c98

Browse files
authored
Merge pull request #9188 from Microsoft/relaxLogicalAnd
Relax && operator typing
2 parents e2376a7 + c9e5bcb commit a3a0c98

File tree

6 files changed

+196
-53
lines changed

6 files changed

+196
-53
lines changed

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2224,7 +2224,8 @@ namespace ts {
22242224

22252225
/* @internal */
22262226
Nullable = Undefined | Null,
2227-
Falsy = String | Number | Boolean | Void | Undefined | Null,
2227+
/* @internal */
2228+
Falsy = Void | Undefined | Null, // TODO: Add false, 0, and ""
22282229
/* @internal */
22292230
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null | Never,
22302231
/* @internal */

tests/baselines/reference/logicalAndOperatorStrictMode.types

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ const a8 = a && z;
8686
>z : string | number | undefined
8787

8888
const s1 = s && a;
89-
>s1 : number[] | string
90-
>s && a : number[] | string
89+
>s1 : number[]
90+
>s && a : number[]
9191
>s : string
9292
>a : number[]
9393

@@ -98,32 +98,32 @@ const s2 = s && s;
9898
>s : string
9999

100100
const s3 = s && x;
101-
>s3 : number | string
102-
>s && x : number | string
101+
>s3 : number
102+
>s && x : number
103103
>s : string
104104
>x : number
105105

106106
const s4 = s && b;
107-
>s4 : boolean | string
108-
>s && b : boolean | string
107+
>s4 : boolean
108+
>s && b : boolean
109109
>s : string
110110
>b : boolean
111111

112112
const s5 = s && v;
113-
>s5 : void | string
114-
>s && v : void | string
113+
>s5 : void
114+
>s && v : void
115115
>s : string
116116
>v : void
117117

118118
const s6 = s && u;
119-
>s6 : string | undefined
120-
>s && u : string | undefined
119+
>s6 : undefined
120+
>s && u : undefined
121121
>s : string
122122
>u : undefined
123123

124124
const s7 = s && n;
125-
>s7 : string | null
126-
>s && n : string | null
125+
>s7 : null
126+
>s && n : null
127127
>s : string
128128
>n : null
129129

@@ -134,14 +134,14 @@ const s8 = s && z;
134134
>z : string | number | undefined
135135

136136
const x1 = x && a;
137-
>x1 : number[] | number
138-
>x && a : number[] | number
137+
>x1 : number[]
138+
>x && a : number[]
139139
>x : number
140140
>a : number[]
141141

142142
const x2 = x && s;
143-
>x2 : string | number
144-
>x && s : string | number
143+
>x2 : string
144+
>x && s : string
145145
>x : number
146146
>s : string
147147

@@ -152,26 +152,26 @@ const x3 = x && x;
152152
>x : number
153153

154154
const x4 = x && b;
155-
>x4 : boolean | number
156-
>x && b : boolean | number
155+
>x4 : boolean
156+
>x && b : boolean
157157
>x : number
158158
>b : boolean
159159

160160
const x5 = x && v;
161-
>x5 : void | number
162-
>x && v : void | number
161+
>x5 : void
162+
>x && v : void
163163
>x : number
164164
>v : void
165165

166166
const x6 = x && u;
167-
>x6 : number | undefined
168-
>x && u : number | undefined
167+
>x6 : undefined
168+
>x && u : undefined
169169
>x : number
170170
>u : undefined
171171

172172
const x7 = x && n;
173-
>x7 : number | null
174-
>x && n : number | null
173+
>x7 : null
174+
>x && n : null
175175
>x : number
176176
>n : null
177177

@@ -182,20 +182,20 @@ const x8 = x && z;
182182
>z : string | number | undefined
183183

184184
const b1 = b && a;
185-
>b1 : number[] | boolean
186-
>b && a : number[] | boolean
185+
>b1 : number[]
186+
>b && a : number[]
187187
>b : boolean
188188
>a : number[]
189189

190190
const b2 = b && s;
191-
>b2 : string | boolean
192-
>b && s : string | boolean
191+
>b2 : string
192+
>b && s : string
193193
>b : boolean
194194
>s : string
195195

196196
const b3 = b && x;
197-
>b3 : number | boolean
198-
>b && x : number | boolean
197+
>b3 : number
198+
>b && x : number
199199
>b : boolean
200200
>x : number
201201

@@ -206,26 +206,26 @@ const b4 = b && b;
206206
>b : boolean
207207

208208
const b5 = b && v;
209-
>b5 : void | boolean
210-
>b && v : void | boolean
209+
>b5 : void
210+
>b && v : void
211211
>b : boolean
212212
>v : void
213213

214214
const b6 = b && u;
215-
>b6 : boolean | undefined
216-
>b && u : boolean | undefined
215+
>b6 : undefined
216+
>b && u : undefined
217217
>b : boolean
218218
>u : undefined
219219

220220
const b7 = b && n;
221-
>b7 : boolean | null
222-
>b && n : boolean | null
221+
>b7 : null
222+
>b && n : null
223223
>b : boolean
224224
>n : null
225225

226226
const b8 = b && z;
227-
>b8 : string | number | boolean | undefined
228-
>b && z : string | number | boolean | undefined
227+
>b8 : string | number | undefined
228+
>b && z : string | number | undefined
229229
>b : boolean
230230
>z : string | number | undefined
231231

@@ -374,44 +374,44 @@ const n8 = n && z;
374374
>z : string | number | undefined
375375

376376
const z1 = z && a;
377-
>z1 : number[] | string | number | undefined
378-
>z && a : number[] | string | number | undefined
377+
>z1 : number[] | undefined
378+
>z && a : number[] | undefined
379379
>z : string | number | undefined
380380
>a : number[]
381381

382382
const z2 = z && s;
383-
>z2 : string | number | undefined
384-
>z && s : string | number | undefined
383+
>z2 : string | undefined
384+
>z && s : string | undefined
385385
>z : string | number | undefined
386386
>s : string
387387

388388
const z3 = z && x;
389-
>z3 : number | string | undefined
390-
>z && x : number | string | undefined
389+
>z3 : number | undefined
390+
>z && x : number | undefined
391391
>z : string | number | undefined
392392
>x : number
393393

394394
const z4 = z && b;
395-
>z4 : boolean | string | number | undefined
396-
>z && b : boolean | string | number | undefined
395+
>z4 : boolean | undefined
396+
>z && b : boolean | undefined
397397
>z : string | number | undefined
398398
>b : boolean
399399

400400
const z5 = z && v;
401-
>z5 : void | string | number
402-
>z && v : void | string | number
401+
>z5 : void
402+
>z && v : void
403403
>z : string | number | undefined
404404
>v : void
405405

406406
const z6 = z && u;
407-
>z6 : string | number | undefined
408-
>z && u : string | number | undefined
407+
>z6 : undefined
408+
>z && u : undefined
409409
>z : string | number | undefined
410410
>u : undefined
411411

412412
const z7 = z && n;
413-
>z7 : string | number | null | undefined
414-
>z && n : string | number | null | undefined
413+
>z7 : null | undefined
414+
>z && n : null | undefined
415415
>z : string | number | undefined
416416
>n : null
417417

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [strictNullLogicalAndOr.ts]
2+
3+
// Repro from #9113
4+
5+
let sinOrCos = Math.random() < .5;
6+
let choice = sinOrCos && Math.sin || Math.cos;
7+
8+
choice(Math.PI);
9+
10+
function sq(n?: number): number {
11+
const r = n !== undefined && n*n || 0;
12+
return r;
13+
}
14+
15+
sq(3);
16+
17+
//// [strictNullLogicalAndOr.js]
18+
// Repro from #9113
19+
var sinOrCos = Math.random() < .5;
20+
var choice = sinOrCos && Math.sin || Math.cos;
21+
choice(Math.PI);
22+
function sq(n) {
23+
var r = n !== undefined && n * n || 0;
24+
return r;
25+
}
26+
sq(3);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=== tests/cases/compiler/strictNullLogicalAndOr.ts ===
2+
3+
// Repro from #9113
4+
5+
let sinOrCos = Math.random() < .5;
6+
>sinOrCos : Symbol(sinOrCos, Decl(strictNullLogicalAndOr.ts, 3, 3))
7+
>Math.random : Symbol(Math.random, Decl(lib.d.ts, --, --))
8+
>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
9+
>random : Symbol(Math.random, Decl(lib.d.ts, --, --))
10+
11+
let choice = sinOrCos && Math.sin || Math.cos;
12+
>choice : Symbol(choice, Decl(strictNullLogicalAndOr.ts, 4, 3))
13+
>sinOrCos : Symbol(sinOrCos, Decl(strictNullLogicalAndOr.ts, 3, 3))
14+
>Math.sin : Symbol(Math.sin, Decl(lib.d.ts, --, --))
15+
>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
16+
>sin : Symbol(Math.sin, Decl(lib.d.ts, --, --))
17+
>Math.cos : Symbol(Math.cos, Decl(lib.d.ts, --, --))
18+
>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
19+
>cos : Symbol(Math.cos, Decl(lib.d.ts, --, --))
20+
21+
choice(Math.PI);
22+
>choice : Symbol(choice, Decl(strictNullLogicalAndOr.ts, 4, 3))
23+
>Math.PI : Symbol(Math.PI, Decl(lib.d.ts, --, --))
24+
>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
25+
>PI : Symbol(Math.PI, Decl(lib.d.ts, --, --))
26+
27+
function sq(n?: number): number {
28+
>sq : Symbol(sq, Decl(strictNullLogicalAndOr.ts, 6, 16))
29+
>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12))
30+
31+
const r = n !== undefined && n*n || 0;
32+
>r : Symbol(r, Decl(strictNullLogicalAndOr.ts, 9, 7))
33+
>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12))
34+
>undefined : Symbol(undefined)
35+
>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12))
36+
>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12))
37+
38+
return r;
39+
>r : Symbol(r, Decl(strictNullLogicalAndOr.ts, 9, 7))
40+
}
41+
42+
sq(3);
43+
>sq : Symbol(sq, Decl(strictNullLogicalAndOr.ts, 6, 16))
44+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
=== tests/cases/compiler/strictNullLogicalAndOr.ts ===
2+
3+
// Repro from #9113
4+
5+
let sinOrCos = Math.random() < .5;
6+
>sinOrCos : boolean
7+
>Math.random() < .5 : boolean
8+
>Math.random() : number
9+
>Math.random : () => number
10+
>Math : Math
11+
>random : () => number
12+
>.5 : number
13+
14+
let choice = sinOrCos && Math.sin || Math.cos;
15+
>choice : (x: number) => number
16+
>sinOrCos && Math.sin || Math.cos : (x: number) => number
17+
>sinOrCos && Math.sin : (x: number) => number
18+
>sinOrCos : boolean
19+
>Math.sin : (x: number) => number
20+
>Math : Math
21+
>sin : (x: number) => number
22+
>Math.cos : (x: number) => number
23+
>Math : Math
24+
>cos : (x: number) => number
25+
26+
choice(Math.PI);
27+
>choice(Math.PI) : number
28+
>choice : (x: number) => number
29+
>Math.PI : number
30+
>Math : Math
31+
>PI : number
32+
33+
function sq(n?: number): number {
34+
>sq : (n?: number | undefined) => number
35+
>n : number | undefined
36+
37+
const r = n !== undefined && n*n || 0;
38+
>r : number
39+
>n !== undefined && n*n || 0 : number
40+
>n !== undefined && n*n : number
41+
>n !== undefined : boolean
42+
>n : number | undefined
43+
>undefined : undefined
44+
>n*n : number
45+
>n : number
46+
>n : number
47+
>0 : number
48+
49+
return r;
50+
>r : number
51+
}
52+
53+
sq(3);
54+
>sq(3) : number
55+
>sq : (n?: number | undefined) => number
56+
>3 : number
57+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @strictNullChecks: true
2+
3+
// Repro from #9113
4+
5+
let sinOrCos = Math.random() < .5;
6+
let choice = sinOrCos && Math.sin || Math.cos;
7+
8+
choice(Math.PI);
9+
10+
function sq(n?: number): number {
11+
const r = n !== undefined && n*n || 0;
12+
return r;
13+
}
14+
15+
sq(3);

0 commit comments

Comments
 (0)