Skip to content

Commit

Permalink
Better typings for Promise.all(), like microsoft#31117
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Aug 23, 2019
1 parent effde01 commit 83331a3
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/lib/es2015.iterable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ interface PromiseConstructor {
* @param values An array of Promises.
* @returns A new Promise.
*/
all<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
all<TAll>(values: Iterable<TAll>): Promise<(TAll extends PromiseLike<infer UAll> ? UAll : TAll)[]>;
}

declare namespace Reflect {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/es2015.promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,79 @@ interface PromiseConstructor {
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4, T5 extends PromiseLike<infer U5> ? U5 : T5, T6 extends PromiseLike<infer U6> ? U6 : T6, T7 extends PromiseLike<infer U7> ? U7 : T7, T8 extends PromiseLike<infer U8> ? U8 : T8, T9 extends PromiseLike<infer U9> ? U9 : T9, T10 extends PromiseLike<infer U10> ? U10 : T10]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1, T2, T3, T4, T5, T6, T7, T8, T9]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4, T5 extends PromiseLike<infer U5> ? U5 : T5, T6 extends PromiseLike<infer U6> ? U6 : T6, T7 extends PromiseLike<infer U7> ? U7 : T7, T8 extends PromiseLike<infer U8> ? U8 : T8, T9 extends PromiseLike<infer U9> ? U9 : T9]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1, T2, T3, T4, T5, T6, T7, T8]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4, T5 extends PromiseLike<infer U5> ? U5 : T5, T6 extends PromiseLike<infer U6> ? U6 : T6, T7 extends PromiseLike<infer U7> ? U7 : T7, T8 extends PromiseLike<infer U8> ? U8 : T8]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
all<T1, T2, T3, T4, T5, T6, T7>(values: [T1, T2, T3, T4, T5, T6, T7]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4, T5 extends PromiseLike<infer U5> ? U5 : T5, T6 extends PromiseLike<infer U6> ? U6 : T6, T7 extends PromiseLike<infer U7> ? U7 : T7]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
all<T1, T2, T3, T4, T5, T6>(values: [T1, T2, T3, T4, T5, T6]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4, T5 extends PromiseLike<infer U5> ? U5 : T5, T6 extends PromiseLike<infer U6> ? U6 : T6]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>;
all<T1, T2, T3, T4, T5>(values: [T1, T2, T3, T4, T5]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4, T5 extends PromiseLike<infer U5> ? U5 : T5]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): Promise<[T1, T2, T3, T4]>;
all<T1, T2, T3, T4>(values: [T1, T2, T3, T4]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3, T4 extends PromiseLike<infer U4> ? U4 : T4]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
all<T1, T2, T3>(values: [T1, T2, T3]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2, T3 extends PromiseLike<infer U3> ? U3 : T3]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
all<T1, T2>(values: [T1, T2]): Promise<[T1 extends PromiseLike<infer U1> ? U1 : T1, T2 extends PromiseLike<infer U2> ? U2 : T2]>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
all<T>(values: T[]): Promise<(T extends PromiseLike<infer U> ? U : T)[]>;

/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
Expand Down Expand Up @@ -120,7 +120,7 @@ interface PromiseConstructor {
* @param value A promise.
* @returns A promise whose internal state matches the provided promise.
*/
resolve<T>(value: T | PromiseLike<T>): Promise<T>;
resolve<T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>;

/**
* Creates a new resolved promise .
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asyncArrowFunction11_es5.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class A {
await Promise.resolve();
>await Promise.resolve() : void
>Promise.resolve() : Promise<void>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }

const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
>obj : { ["a"]: () => this; }
Expand Down
48 changes: 24 additions & 24 deletions tests/baselines/reference/asyncFunctionReturnType.types
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ async function fIndexedTypeForPromiseOfStringProp(obj: Obj): Promise<Obj["string

return Promise.resolve(obj.stringProp);
>Promise.resolve(obj.stringProp) : Promise<string>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.stringProp : string
>obj : Obj
>stringProp : string
Expand All @@ -58,9 +58,9 @@ async function fIndexedTypeForExplicitPromiseOfStringProp(obj: Obj): Promise<Obj

return Promise.resolve<Obj["stringProp"]>(obj.stringProp);
>Promise.resolve<Obj["stringProp"]>(obj.stringProp) : Promise<string>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.stringProp : string
>obj : Obj
>stringProp : string
Expand All @@ -82,9 +82,9 @@ async function fIndexedTypeForPromiseOfAnyProp(obj: Obj): Promise<Obj["anyProp"]

return Promise.resolve(obj.anyProp);
>Promise.resolve(obj.anyProp) : Promise<any>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.anyProp : any
>obj : Obj
>anyProp : any
Expand All @@ -96,9 +96,9 @@ async function fIndexedTypeForExplicitPromiseOfAnyProp(obj: Obj): Promise<Obj["a

return Promise.resolve<Obj["anyProp"]>(obj.anyProp);
>Promise.resolve<Obj["anyProp"]>(obj.anyProp) : Promise<any>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.anyProp : any
>obj : Obj
>anyProp : any
Expand All @@ -120,9 +120,9 @@ async function fGenericIndexedTypeForPromiseOfStringProp<TObj extends Obj>(obj:

return Promise.resolve(obj.stringProp);
>Promise.resolve(obj.stringProp) : Promise<string>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.stringProp : string
>obj : TObj
>stringProp : string
Expand All @@ -133,10 +133,10 @@ async function fGenericIndexedTypeForExplicitPromiseOfStringProp<TObj extends Ob
>obj : TObj

return Promise.resolve<TObj["stringProp"]>(obj.stringProp);
>Promise.resolve<TObj["stringProp"]>(obj.stringProp) : Promise<TObj["stringProp"]>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve<TObj["stringProp"]>(obj.stringProp) : Promise<TObj["stringProp"] extends PromiseLike<infer U> ? U : TObj["stringProp"]>
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.stringProp : string
>obj : TObj
>stringProp : string
Expand All @@ -158,9 +158,9 @@ async function fGenericIndexedTypeForPromiseOfAnyProp<TObj extends Obj>(obj: TOb

return Promise.resolve(obj.anyProp);
>Promise.resolve(obj.anyProp) : Promise<any>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.anyProp : any
>obj : TObj
>anyProp : any
Expand All @@ -171,10 +171,10 @@ async function fGenericIndexedTypeForExplicitPromiseOfAnyProp<TObj extends Obj>(
>obj : TObj

return Promise.resolve<TObj["anyProp"]>(obj.anyProp);
>Promise.resolve<TObj["anyProp"]>(obj.anyProp) : Promise<TObj["anyProp"]>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve<TObj["anyProp"]>(obj.anyProp) : Promise<TObj["anyProp"] extends PromiseLike<infer U> ? U : TObj["anyProp"]>
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj.anyProp : any
>obj : TObj
>anyProp : any
Expand All @@ -197,10 +197,10 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends
>key : K

return Promise.resolve(obj[key]);
>Promise.resolve(obj[key]) : Promise<TObj[K]>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve(obj[key]) : Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]>
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj[key] : TObj[K]
>obj : TObj
>key : K
Expand All @@ -212,10 +212,10 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>key : K

return Promise.resolve<TObj[K]>(obj[key]);
>Promise.resolve<TObj[K]>(obj[key]) : Promise<TObj[K]>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise.resolve<TObj[K]>(obj[key]) : Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]>
>Promise.resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>resolve : { <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; (): Promise<void>; }
>obj[key] : TObj[K]
>obj : TObj
>key : K
Expand Down
Loading

0 comments on commit 83331a3

Please sign in to comment.