Skip to content

conditional typing of resolve() in promise constructor#21867

Closed
tvald wants to merge 3 commits into
microsoft:masterfrom
tvald-contrib:promise
Closed

conditional typing of resolve() in promise constructor#21867
tvald wants to merge 3 commits into
microsoft:masterfrom
tvald-contrib:promise

Conversation

@tvald
Copy link
Copy Markdown

@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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