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

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

Comments

Projects
None yet
2 participants
@bfred-it
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

Suggestion

Currently:

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>'

Checklist

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.
@bfred-it

This comment has been minimized.

Copy link
Author

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: https://www.typescriptlang.org/play/#code/MYewdgzgLgBAtgUwhAhgcwTAvDMCDuMACgE4hwCWECAFCUiADYBumWAfDPRE6zQCwBKQUA

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

@fatcerberus

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.

@bfred-it

This comment has been minimized.

Copy link
Author

commented Jul 4, 2019

That's weird, I'm almost pretty sure I did that once: https://github.com/bfred-it/webext-options-sync/blob/cbf837e6b2bd93e0e39fe0d43a8b47cdde9e53fd/source/index.ts#L66-L68

(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.