Skip to content

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

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

Closed
AlCalzone opened this issue Oct 18, 2020 · 2 comments · Fixed by #40536
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Help Wanted You can do this
Milestone

Comments

@AlCalzone
Copy link
Contributor

AlCalzone commented Oct 18, 2020

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:

@MartinJohns
Copy link
Contributor

Possibly related: #40494

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 19, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 19, 2020
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Oct 19, 2020
@Kingwl
Copy link
Contributor

Kingwl commented Oct 20, 2020

Yes. It's the same reason of #40494

You can try this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants