Skip to content

conditional typing of resolve() in promise constructor #21867

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
wants to merge 3 commits into from

Conversation

tvald
Copy link

@tvald tvald commented Feb 11, 2018

Fixes #11094, #3356. Replaces the approach in #19633 with a conditional typing of resolve():

resolve: T extends void
  ? (value?: PromiseLike<void>) => void
  : (value: T | PromiseLike<T>) => void

Much thanks to @ahejlsberg and @mhegazy for resolving #21756 and #21823 to make this possible!

@mhegazy
Copy link
Contributor

mhegazy commented Feb 12, 2018

@rbuckton can you take a look

@mhegazy mhegazy requested a review from rbuckton February 12, 2018 19:52
@falsandtru
Copy link
Contributor

falsandtru commented Mar 21, 2018

@tvald When T is a union type, resolve function won't be callable. You have to do just as follows.

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

@falsandtru
Copy link
Contributor

My previous code is not extensible for classes in the current compiler. Fixed as follows.

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

@mhegazy
Copy link
Contributor

mhegazy commented Apr 19, 2018

Should be addressed by #22772

@mhegazy mhegazy closed this Apr 19, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 30, 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 this pull request may close these issues.

Promise can be resolved with undefined but not reflected in type
3 participants