Skip to content

Fix #32809 #33199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18027,7 +18027,7 @@ namespace ts {
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
// declaration container are the same).
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) ||
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.AnyOrUnknown) !== 0 ||
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
node.parent.kind === SyntaxKind.NonNullExpression ||
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const v5 = v && v;
>v5 : void
>v && v : void
>v : void
>v : never
>v : void

const v6 = v && u;
>v6 : void
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/voidIsInitialized.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [voidIsInitialized.ts]
const x: void = undefined;
const y: void = undefined;

if(typeof x === "undefined") {
x // no error: assume x2 is initialised
}

if(typeof y !== "undefined") {
y // no error: do not narrow void
}


//// [voidIsInitialized.js]
"use strict";
var x = undefined;
var y = undefined;
if (typeof x === "undefined") {
x; // no error: assume x2 is initialised
}
if (typeof y !== "undefined") {
y; // no error: do not narrow void
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/voidIsInitialized.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/voidIsInitialized.ts ===
const x: void = undefined;
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
>undefined : Symbol(undefined)

const y: void = undefined;
>y : Symbol(y, Decl(voidIsInitialized.ts, 1, 5))
>undefined : Symbol(undefined)

if(typeof x === "undefined") {
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))

x // no error: assume x2 is initialised
>x : Symbol(x, Decl(voidIsInitialized.ts, 0, 5))
}

if(typeof y !== "undefined") {
>y : Symbol(y, Decl(voidIsInitialized.ts, 1, 5))

y // no error: do not narrow void
>y : Symbol(y, Decl(voidIsInitialized.ts, 1, 5))
}

29 changes: 29 additions & 0 deletions tests/baselines/reference/voidIsInitialized.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/voidIsInitialized.ts ===
const x: void = undefined;
>x : void
>undefined : undefined

const y: void = undefined;
>y : void
>undefined : undefined

if(typeof x === "undefined") {
>typeof x === "undefined" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : void
>"undefined" : "undefined"

x // no error: assume x2 is initialised
>x : void
}

if(typeof y !== "undefined") {
>typeof y !== "undefined" : boolean
>typeof y : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>y : void
>"undefined" : "undefined"

y // no error: do not narrow void
>y : void
}

12 changes: 12 additions & 0 deletions tests/cases/compiler/voidIsInitialized.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @strict: true

const x: void = undefined;
const y: void = undefined;

if(typeof x === "undefined") {
x // no error: assume x2 is initialised
}

if(typeof y !== "undefined") {
y // no error: do not narrow void
}