Skip to content

Commit 7435425

Browse files
author
Orta
authored
Merge pull request #33199 from jack-williams/fix-32809
Fix #32809
2 parents d7c83f0 + 630499e commit 7435425

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18059,7 +18059,7 @@ namespace ts {
1805918059
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
1806018060
// declaration container are the same).
1806118061
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) ||
18062-
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.AnyOrUnknown) !== 0 ||
18062+
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
1806318063
isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
1806418064
node.parent.kind === SyntaxKind.NonNullExpression ||
1806518065
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||

tests/baselines/reference/logicalAndOperatorStrictMode.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ const v5 = v && v;
256256
>v5 : void
257257
>v && v : void
258258
>v : void
259-
>v : never
259+
>v : void
260260

261261
const v6 = v && u;
262262
>v6 : void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [voidIsInitialized.ts]
2+
const x: void = undefined;
3+
const y: void = undefined;
4+
5+
if(typeof x === "undefined") {
6+
x // no error: assume x2 is initialised
7+
}
8+
9+
if(typeof y !== "undefined") {
10+
y // no error: do not narrow void
11+
}
12+
13+
14+
//// [voidIsInitialized.js]
15+
"use strict";
16+
var x = undefined;
17+
var y = undefined;
18+
if (typeof x === "undefined") {
19+
x; // no error: assume x2 is initialised
20+
}
21+
if (typeof y !== "undefined") {
22+
y; // no error: do not narrow void
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/voidIsInitialized.ts ===
2+
const x: void = undefined;
3+
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
4+
>undefined : Symbol(undefined)
5+
6+
const y: void = undefined;
7+
>y : Symbol(y, Decl(voidIsInitialized.ts, 1, 5))
8+
>undefined : Symbol(undefined)
9+
10+
if(typeof x === "undefined") {
11+
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
12+
13+
x // no error: assume x2 is initialised
14+
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
15+
}
16+
17+
if(typeof y !== "undefined") {
18+
>y : Symbol(y, Decl(voidIsInitialized.ts, 1, 5))
19+
20+
y // no error: do not narrow void
21+
>y : Symbol(y, Decl(voidIsInitialized.ts, 1, 5))
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/voidIsInitialized.ts ===
2+
const x: void = undefined;
3+
>x : void
4+
>undefined : undefined
5+
6+
const y: void = undefined;
7+
>y : void
8+
>undefined : undefined
9+
10+
if(typeof x === "undefined") {
11+
>typeof x === "undefined" : boolean
12+
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
13+
>x : void
14+
>"undefined" : "undefined"
15+
16+
x // no error: assume x2 is initialised
17+
>x : void
18+
}
19+
20+
if(typeof y !== "undefined") {
21+
>typeof y !== "undefined" : boolean
22+
>typeof y : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
23+
>y : void
24+
>"undefined" : "undefined"
25+
26+
y // no error: do not narrow void
27+
>y : void
28+
}
29+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @strict: true
2+
3+
const x: void = undefined;
4+
const y: void = undefined;
5+
6+
if(typeof x === "undefined") {
7+
x // no error: assume x2 is initialised
8+
}
9+
10+
if(typeof y !== "undefined") {
11+
y // no error: do not narrow void
12+
}

0 commit comments

Comments
 (0)