Skip to content

Aliased Promise types behave differently than the original Promise with target es5 #39282

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
theRobinator opened this issue Jun 26, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@theRobinator
Copy link

Search Terms: promise type, not a valid async function return type in es5/es3, alias type

Code

On the latest TS (3.9.2 as of this posting) target set to es5, these two code snippets behave differently:

Everything works fine here:

async function foo(): Promise<string> {
    return 'hello';
}

Adding this type alias breaks it:

type StringPromise = Promise<string>;

async function foo(): StringPromise {
    return 'hello';
}

Expected behavior: Adding a type declaration should not affect the behavior of the example. An error should be thrown in both cases or neither.

Actual behavior: Adding a type declaration causes the error Type 'StringPromise' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

Playground Link. I added a dummy Promise constructor in there to "polyfill" the lib, but this also reproduces on disk.

@MartinJohns
Copy link
Contributor

Duplicate of #32574.

@yortus
Copy link
Contributor

yortus commented Jun 27, 2020

The reason this seems surprising is that for async functions in ES3/ES5, TypeScript takes the identifier in the return type annotation and emits it into the runtime code. It's probably the only case where TS does that.

You can see it in this playground link. StringPromise appears in a value position in the emitted code, and changing or removing the type annotation will change the emitted code.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 30, 2020
@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

5 participants