Skip to content

Logical nullish assignment together with nullish coalescing produces false positive for "use before assignment" #41153

Closed
@AlCalzone

Description

@AlCalzone

TypeScript Version: 4.1.0-dev.20201015

Search Terms: logical nullish assignment use before assigned

Code

let x: boolean;
declare function getX(): boolean;
declare function maybeGetX(): boolean | undefined;

if (Math.random() > 0.5) {
	x = getX();
}

x; // x is used before being assigned (expected)

x ??= maybeGetX() ?? true;

x; // Error: x is used before being assigned (UNEXPECTED)

Expected behavior:
No error in the last line

Actual behavior:
Error: x is used before being assigned

Playground Link: Playground

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions