Skip to content

Narrowing ??= assignment doesn't work when RHS contains ?? #44732

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
tomtheisen opened this issue Jun 24, 2021 · 4 comments
Closed

Narrowing ??= assignment doesn't work when RHS contains ?? #44732

tomtheisen opened this issue Jun 24, 2021 · 4 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@tomtheisen
Copy link

Bug Report

🔎 Search Terms

Control-Flow-Analysis Coalescing

🕗 Version & Regression Information

  • The compiler is reporting a type error incorrectly.
  • This changed between versions 3.9.7 and 4.0.5.
  • Prior to 4.0, the syntax ??= wasn't supported.
  • It is unchanged through 4.4.0 nightly.

⏯ Playground Link

Playground link with relevant code

💻 Code

function f1(a?: {}) : {} {
    const n = null ?? {};
    a ??= n;
    return a; // no problem
}

function f2(a?: {}) : {} {
    a ??= (null ?? {});
    return a; // Type '{} | undefined' is not assignable to type '{}'.
}

function identityFunction<T>(a: T): T {
    return a;
}
function f3(a?: {}) : {} {
    a ??= identityFunction(null ?? {});
    return a; // also fine
}

🙁 Actual behavior

On line 9, a type error is reported: "Type '{} | undefined' is not assignable to type '{}'."

🙂 Expected behavior

I would expect that the code should pass type checking. Here's why.

1 . ??= generally supports narrowing types, as shown in the playground link.
2. ?? generally supports nullability narrowing, as shown in the playground link.
3. There's nothing special about a RHS of a ??= that contains a ??.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 24, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 24, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jun 24, 2021
@danielmayorga
Copy link

I believe this is also applicable to the logical OR assignment ||=. The code in the playground mirrors the code and conditions stated above.

Playground link with relevant code

I also believe there are situations where logical AND assignment &&= is inconsistent as well.

Playground link with relevant code

I'm trying to look through the codebase to learn about it and how to fix this issue, and stumbled across these cases as well. Don't want to open a new work item since they are so closely related. Let me know if there is a better procedure to report this.

@fatcerberus
Copy link

I believe this is also applicable to the logical OR assignment ||=. The code in the playground mirrors the code and conditions stated above.

If so, this is probably related to #44553.

@AlCalzone
Copy link
Contributor

Duplicate of #41153 and possibly #40494 ?

@tomtheisen
Copy link
Author

Seems like this is fixed, as of 5.6.2

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

No branches or pull requests

5 participants