Skip to content

Commit a82bd5b

Browse files
committed
Assume void variables are initialized
1 parent f41472b commit a82bd5b

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18027,7 +18027,7 @@ namespace ts {
1802718027
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
1802818028
// declaration container are the same).
1802918029
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) ||
18030-
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.AnyOrUnknown) !== 0 ||
18030+
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
1803118031
isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
1803218032
node.parent.kind === SyntaxKind.NonNullExpression ||
1803318033
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,14 @@
1+
//// [voidIsInitialized.ts]
2+
const x: void = undefined;
3+
4+
if(typeof x === "undefined") {
5+
x // no error
6+
}
7+
8+
9+
//// [voidIsInitialized.js]
10+
"use strict";
11+
var x = undefined;
12+
if (typeof x === "undefined") {
13+
x; // no error
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
if(typeof x === "undefined") {
7+
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
8+
9+
x // no error
10+
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/voidIsInitialized.ts ===
2+
const x: void = undefined;
3+
>x : void
4+
>undefined : undefined
5+
6+
if(typeof x === "undefined") {
7+
>typeof x === "undefined" : boolean
8+
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
9+
>x : void
10+
>"undefined" : "undefined"
11+
12+
x // no error
13+
>x : void
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @strict: true
2+
3+
const x: void = undefined;
4+
5+
if(typeof x === "undefined") {
6+
x // no error
7+
}

0 commit comments

Comments
 (0)