Skip to content

Commit

Permalink
Better typings for Promise.resolve(), like microsoft#31117
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko authored and rbuckton committed Mar 18, 2022
1 parent df1faa0 commit 88254eb
Show file tree
Hide file tree
Showing 41 changed files with 290 additions and 265 deletions.
2 changes: 1 addition & 1 deletion src/lib/es2015.promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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>;
}

declare var Promise: PromiseConstructor;
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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }

const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
>obj : { a: () => this; }
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/asyncFunctionReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ async function fGenericIndexedTypeForKProp<TObj extends Obj, K extends keyof TOb
return obj[key];
}

async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
return Promise.resolve(obj[key]);
}

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
return Promise.resolve<TObj[K]>(obj[key]);
}
}


//// [asyncFunctionReturnType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
Expand Down
15 changes: 13 additions & 2 deletions tests/baselines/reference/asyncFunctionReturnType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ async function fGenericIndexedTypeForKProp<TObj extends Obj, K extends keyof TOb
>key : Symbol(key, Decl(asyncFunctionReturnType.ts, 64, 93))
}

async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
>fGenericIndexedTypeForPromiseOfKProp : Symbol(fGenericIndexedTypeForPromiseOfKProp, Decl(asyncFunctionReturnType.ts, 66, 1))
>TObj : Symbol(TObj, Decl(asyncFunctionReturnType.ts, 68, 52))
>Obj : Symbol(Obj, Decl(asyncFunctionReturnType.ts, 8, 1))
Expand All @@ -252,6 +252,11 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends
>K : Symbol(K, Decl(asyncFunctionReturnType.ts, 68, 69))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>TObj : Symbol(TObj, Decl(asyncFunctionReturnType.ts, 68, 52))
>K : Symbol(K, Decl(asyncFunctionReturnType.ts, 68, 69))
>PromiseLike : Symbol(PromiseLike, Decl(lib.es5.d.ts, --, --))
>U : Symbol(U, Decl(asyncFunctionReturnType.ts, 68, 153))
>U : Symbol(U, Decl(asyncFunctionReturnType.ts, 68, 153))
>TObj : Symbol(TObj, Decl(asyncFunctionReturnType.ts, 68, 52))
>K : Symbol(K, Decl(asyncFunctionReturnType.ts, 68, 69))

return Promise.resolve(obj[key]);
Expand All @@ -262,7 +267,7 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends
>key : Symbol(key, Decl(asyncFunctionReturnType.ts, 68, 102))
}

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
>fGenericIndexedTypeForExplicitPromiseOfKProp : Symbol(fGenericIndexedTypeForExplicitPromiseOfKProp, Decl(asyncFunctionReturnType.ts, 70, 1))
>TObj : Symbol(TObj, Decl(asyncFunctionReturnType.ts, 72, 60))
>Obj : Symbol(Obj, Decl(asyncFunctionReturnType.ts, 8, 1))
Expand All @@ -274,6 +279,11 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>K : Symbol(K, Decl(asyncFunctionReturnType.ts, 72, 77))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>TObj : Symbol(TObj, Decl(asyncFunctionReturnType.ts, 72, 60))
>K : Symbol(K, Decl(asyncFunctionReturnType.ts, 72, 77))
>PromiseLike : Symbol(PromiseLike, Decl(lib.es5.d.ts, --, --))
>U : Symbol(U, Decl(asyncFunctionReturnType.ts, 72, 161))
>U : Symbol(U, Decl(asyncFunctionReturnType.ts, 72, 161))
>TObj : Symbol(TObj, Decl(asyncFunctionReturnType.ts, 72, 60))
>K : Symbol(K, Decl(asyncFunctionReturnType.ts, 72, 77))

return Promise.resolve<TObj[K]>(obj[key]);
Expand All @@ -285,3 +295,4 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>obj : Symbol(obj, Decl(asyncFunctionReturnType.ts, 72, 100))
>key : Symbol(key, Decl(asyncFunctionReturnType.ts, 72, 110))
}

57 changes: 29 additions & 28 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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve<TObj["stringProp"]>(obj.stringProp) : Promise<TObj["stringProp"] extends PromiseLike<infer U> ? U : TObj["stringProp"]>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>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 : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve<TObj["anyProp"]>(obj.anyProp) : Promise<TObj["anyProp"] extends PromiseLike<infer U> ? U : TObj["anyProp"]>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>obj.anyProp : any
>obj : TObj
>anyProp : any
Expand All @@ -191,32 +191,33 @@ async function fGenericIndexedTypeForKProp<TObj extends Obj, K extends keyof TOb
>key : K
}

async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
>fGenericIndexedTypeForPromiseOfKProp : <TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K) => Promise<TObj[K]>
async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
>fGenericIndexedTypeForPromiseOfKProp : <TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K) => Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]>
>obj : TObj
>key : K

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

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
>fGenericIndexedTypeForExplicitPromiseOfKProp : <TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K) => Promise<TObj[K]>
async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
>fGenericIndexedTypeForExplicitPromiseOfKProp : <TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K) => Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]>
>obj : TObj
>key : K

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ function foo() {
>await(Promise.resolve(1)) : any
>await : any
>Promise.resolve(1) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>1 : 1

return foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const nums = [1, 2, 3].map(n => Promise.resolve(n))
>n => Promise.resolve(n) : (n: number) => Promise<number>
>n : number
>Promise.resolve(n) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>n : number

class C {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const nums = [1, 2, 3].map(n => Promise.resolve(n))
>n => Promise.resolve(n) : (n: number) => Promise<number>
>n : number
>Promise.resolve(n) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
>resolve : { (): Promise<void>; <T>(value: T): Promise<T extends PromiseLike<infer U> ? U : T>; }
>n : number

class C {
Expand Down
Loading

0 comments on commit 88254eb

Please sign in to comment.