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

Improve Promise constructor return type #32254

bfred-it opened this issue Jul 4, 2019 · 3 comments


None yet
2 participants
Copy link

commented Jul 4, 2019

I tried looking for this, but didn't find anything related to it.

Search Terms

new promise constructor return type generic inferred



const promised4 = new Promise(resolve => resolve(4));
// typeof promised4 === 'Promise<unknown>'
// it should be `Promise<4>'

You can already use the generic for this, but this can be automated.

const promised4 = new Promise<4>(resolve => resolve(4));
// typeof promised4 === 'Promise<4>'


My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code

Note: it would be a good break because Promises become typed.

  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

This comment has been minimized.

Copy link

commented Jul 4, 2019

It looks like the type should to that already, but I'm testing on 3.6.0-dev.20190704 and new Promise(...) always returns Promise<unknown>

new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;

You can also replicate the issue on the repl:

But strangely the definition is different

@bfred-it bfred-it changed the title Improve new Promise constructor return type Improve Promise constructor return type Jul 4, 2019


This comment has been minimized.

Copy link

commented Jul 4, 2019

This is a known limitation of the type inference machinery - it can't infer the promise's T from the call to resolve() (i.e. the contents of a callback don't contribute to type inference). For new Promise you unfortunately do have to provide the type of the promise explicitly.


This comment has been minimized.

Copy link

commented Jul 4, 2019

That's weird, I'm almost pretty sure I did that once:

(The types for that function ultimately failed because I couldn't type args correcly.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.