-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add Awaited<T> to core library #21613
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1273,7 +1273,20 @@ declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) | |
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void; | ||
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void; | ||
|
||
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>; | ||
/** An object type that can be definitely be awaited. Do not inherit from this type. */ | ||
declare type Awaitable<T> = { then(onfulfilled: (value: T) => any): any; }; | ||
|
||
/** An object type that may not be awaitable. Do not inherit from this type. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this type isn't supposed to be extended/used, why not just inline the object type inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've considered that, but this is more readable when getting Quick Info on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to also remove |
||
declare type NonAwaitable = { then(...args: any[]): any; }; | ||
|
||
/** Gets the type resulting from awaiting `T`. This does **not** recursively unwrap nested promises. */ | ||
declare type Awaited<T> = | ||
T extends Awaitable<Awaitable<infer U>> ? U : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we figure that two levels deep is enough for most uses, and if anyone complains, we can always add more later? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess if we currently don't do better than two levels, this will do for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually only need: declare type Awaited<T> =
T extends Awaitable<infer U> ? U :
T extends { then(...args: any[]): any; } ? never :
T; I seemed to have forgotten to stage an additional change to es5.d.ts. |
||
T extends Awaitable<infer U> ? U : | ||
T extends NonAwaitable ? never : | ||
T; | ||
|
||
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | Awaitable<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>; | ||
|
||
interface PromiseLike<T> { | ||
/** | ||
|
@@ -1282,7 +1295,7 @@ interface PromiseLike<T> { | |
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>; | ||
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: Awaited<T>) => TResult1) | null, onrejected?: ((reason: any) => TResult2) | null): PromiseLike<Awaited<TResult1 | TResult2>>; | ||
} | ||
|
||
/** | ||
|
@@ -1295,14 +1308,14 @@ interface Promise<T> { | |
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>; | ||
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: Awaited<T>) => TResult1) | null, onrejected?: ((reason: any) => TResult2) | null): Promise<Awaited<TResult1 | TResult2>>; | ||
|
||
/** | ||
* Attaches a callback for only the rejection of the Promise. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>; | ||
catch<TResult = never>(onrejected?: ((reason: any) => TResult) | null): Promise<Awaited<T | TResult>>; | ||
} | ||
|
||
interface ArrayLike<T> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"be definitely be"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed