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

awaiting a Promise recasts top variable to initial type #19918

Closed
bondz opened this issue Nov 10, 2017 · 2 comments · Fixed by #56908
Closed

awaiting a Promise recasts top variable to initial type #19918

bondz opened this issue Nov 10, 2017 · 2 comments · Fixed by #56908

Comments

@bondz
Copy link

bondz commented Nov 10, 2017

TypeScript Version: 2.6.1, 2.7.0-dev.20171110

function test(a: boolean): false | Date {
    if (a) {
        return new Date();
    }

    return false;
}


async function test2() {
    let b = test(true);

    if (!b) {
        b = new Date();
    }
    
    await Promise.all([]).then(() => {
        b.getDate(); //Should compile.
            
    })
}

Expected behavior:
Should compile
Actual behavior:
Throws with Property 'getDate' does not exist on type 'true'.


This can be reproduced with any return type and even when type inference is used.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 10, 2017

Likely duplicate of #13180. Also, see #9998 for detailed commentary and discussion.

Note: this is not related to promises.

Simple fix:

async function test2() {
  const b = test(true) || new Date();
  await Promise.all([]).then(() => {
      b.getDate(); // compiles.
  });
}

@bondz
Copy link
Author

bondz commented Nov 10, 2017

Thanks @aluanhaddad. The simple fix works and looks like #9998 should cover this. Closing this ticket.

@bondz bondz closed this as completed Nov 10, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants