Skip to content
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

Value can change "during" await, but compiler doesn't seem to know that #46439

Closed
EvanHahn opened this issue Oct 20, 2021 · 4 comments
Closed

Comments

@EvanHahn
Copy link

Bug Report

I'm receiving the following error...

This condition will always return 'false' since the types '"b"' and '"a"' have no overlap.(2367)

...but the value might be "a". The condition might not return false. It's worth looking at the code snippets below, as I think they describe the issue better.

🔎 Search Terms

  • TS2367
  • Condition always returns false
  • Value changes during await

🕗 Version & Regression Information

This does not seem to be a recent regression. When I test in the playground, I see this problem in all v4 versions.

⏯ Playground Link

Playground link with relevant code

💻 Code

let state: 'a' | 'b' = 'a';

async function a() {
    state = 'b';

    await Promise.resolve();

    // The following line has an error that I think should not happen:
    if (state === 'a') {
        throw new Error('hello');
    }
}

a()
state = 'a';

Here's another example that I think better isolates the issue:

let state: 'a' | 'b' = 'a';

async function a() {
    state = 'b';

    await Promise.resolve();

    const newState: 'a' | 'b' = state;
    console.log(newState); // <- type seems incorrect here
}

a()
state = 'a';

🙁 Actual behavior

Got TS2367.

🙂 Expected behavior

There is no error.

@jcalz
Copy link
Contributor

jcalz commented Oct 20, 2021

We got another #9998, folks!

@fatcerberus
Copy link

Just want to point out that this is not specific to await; any function call might change non-local state.

@EvanHahn
Copy link
Author

Maybe I should close this as a duplicate of #9998?

@EvanHahn
Copy link
Author

Looks like many similar issues were closed as duplicates of #9998. I'll close this one, too.

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

3 participants