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

Variable incorrectly inferred across await boundary #31009

Closed
sceutre opened this issue Apr 18, 2019 · 3 comments
Closed

Variable incorrectly inferred across await boundary #31009

sceutre opened this issue Apr 18, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@sceutre
Copy link

sceutre commented Apr 18, 2019

TypeScript Version: 3.4.0

Search Terms: enumeration async await

Code

function wait(tm: number) {
   return new Promise((resolve, reject) => {
      setTimeout(() => resolve(true), tm);
   });
}

let x: "foo" | "bar";

async function baz() {
    x = "bar";
    await wait(1000);
    if (x == "foo") {
        // erroneous: This condition will always return 'false' since the types '"bar"' and '"foo"' have no overlap.
        console.log("could be reached");
    }
}

setTimeout(() => x = "foo", 500);
baz();

Expected behavior:

Code should compile fine

Actual behavior:

Compiler believes x must be "bar" since it was assigned such at the start of the method. However since there is an await this is not true.

Playground Link:

Playground link

Related Issues:

@MartinJohns
Copy link
Contributor

Duplicate of #9998.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 18, 2019
@sceutre
Copy link
Author

sceutre commented Apr 19, 2019

Ah I had seen that issue but didn't read the comments in enough detail. In particular this issue is exactly what was written up in this comment: #9998 (comment)

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants