Skip to content

Invalid 'Object is possibly undefined' for variable definitely assigned via compound assignment #42412

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
ajafff opened this issue Jan 19, 2021 · 1 comment

Comments

@ajafff
Copy link
Contributor

ajafff commented Jan 19, 2021

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

  • This is the behavior since 4.0 where compound assignments were introduced

⏯ Playground Link

Playground Link: Provided

πŸ’» Code

function foo(a: string[] | undefined, b: string[] | undefined) {
    a ??= b || []; // doesn't work
    a ??= b ?? []; // doesn't work
    a ||= (b || []); // doesn't work
    a ||= b || []; // doesn't work
    // a ??= b!; // works, so it seems to require the BarBar or QuestionQuestion operator on the RHS
    // a ?? (a = b || []); // works
    // a || (a = b || []); // works
    // if (!a) a = b || []; // works
    a.includes('foo'); // 'a' is possibly 'undefined'
}

πŸ™ Actual behavior

Error Object is possibly 'undefined'. on the last statement.

πŸ™‚ Expected behavior

Each of the assignments should remove undefined from the type.

This might be caused by a faulty control flow graph for compound assingments because the same code written as its downlevel equivalent (see commented out lines) works as expected.

/cc @Kingwl

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 19, 2021

Looks like a duplicate of #40494. Tho according to that issue there's a fix available for 4.2.0 already, which is not working on the nightly in the playground.

@ajafff ajafff closed this as completed Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants