Skip to content

Commit

Permalink
Better typings for Promise.then(), like microsoft#31117
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Feb 21, 2020
1 parent d0c961e commit 5535587
Show file tree
Hide file tree
Showing 58 changed files with 993 additions and 990 deletions.
6 changes: 3 additions & 3 deletions src/lib/es5.d.ts
Expand Up @@ -1387,7 +1387,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: T) => TResult1) | undefined | null, onrejected?: ((reason: any) => TResult2) | undefined | null): PromiseLike<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>;
}

/**
Expand All @@ -1400,14 +1400,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: T) => TResult1) | undefined | null, onrejected?: ((reason: any) => TResult2) | undefined | null): Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : 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) | undefined | null): Promise<T | (TResult extends PromiseLike<infer UResult> ? UResult : TResult)>;
}

interface ArrayLike<T> {
Expand Down
Expand Up @@ -7,7 +7,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(10,23): error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
The types returned by 'then(...)' are incompatible between these types.
Type 'void' is not assignable to type 'PromiseLike<TResult1 | TResult2>'.
Type 'void' is not assignable to type 'PromiseLike<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>'.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(23,25): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.

Expand Down Expand Up @@ -39,7 +39,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
!!! error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
!!! error TS1055: Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
!!! error TS1055: The types returned by 'then(...)' are incompatible between these types.
!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike<TResult1 | TResult2>'.
!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>'.
async function fn7() { return; } // valid: Promise<void>
async function fn8() { return 1; } // valid: Promise<number>
async function fn9() { return null; } // valid: Promise<any>
Expand Down
Expand Up @@ -28,10 +28,10 @@ export let lazyCard = () => import('./Card').then(a => a.default);
>lazyCard : () => Promise<(suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }>
>() => import('./Card').then(a => a.default) : () => Promise<(suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }>
>import('./Card').then(a => a.default) : Promise<(suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }>
>import('./Card').then : <TResult1 = typeof import("tests/cases/compiler/Card"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/compiler/Card")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>import('./Card').then : <TResult1 = typeof import("tests/cases/compiler/Card"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/compiler/Card")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>import('./Card') : Promise<typeof import("tests/cases/compiler/Card")>
>'./Card' : "./Card"
>then : <TResult1 = typeof import("tests/cases/compiler/Card"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/compiler/Card")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = typeof import("tests/cases/compiler/Card"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/compiler/Card")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>a => a.default : (a: typeof import("tests/cases/compiler/Card")) => (suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }
>a : typeof import("tests/cases/compiler/Card")
>a.default : (suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/esModuleInteropImportCall.types
Expand Up @@ -9,10 +9,10 @@ export = foo;
=== tests/cases/compiler/index.ts ===
import("./foo").then(f => {
>import("./foo").then(f => { f.default;}) : Promise<void>
>import("./foo").then : <TResult1 = { default: () => void; }, TResult2 = never>(onfulfilled?: (value: { default: () => void; }) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>import("./foo").then : <TResult1 = { default: () => void; }, TResult2 = never>(onfulfilled?: (value: { default: () => void; }) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>import("./foo") : Promise<{ default: () => void; }>
>"./foo" : "./foo"
>then : <TResult1 = { default: () => void; }, TResult2 = never>(onfulfilled?: (value: { default: () => void; }) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = { default: () => void; }, TResult2 = never>(onfulfilled?: (value: { default: () => void; }) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>f => { f.default;} : (f: { default: () => void; }) => void
>f : { default: () => void; }

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/genericFunctionInference1.types
Expand Up @@ -842,9 +842,9 @@ const promise = Promise.resolve(1);

promise.then(
>promise.then( pipe( x => x + 1, x => x * 2, ),) : Promise<number>
>promise.then : <TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
>promise.then : <TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1) | null | undefined, onrejected?: ((reason: any) => TResult2) | null | undefined) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>promise : Promise<number>
>then : <TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
>then : <TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1) | null | undefined, onrejected?: ((reason: any) => TResult2) | null | undefined) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>

pipe(
>pipe( x => x + 1, x => x * 2, ) : (x: number) => number
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/importCallExpression1ES2020.types
Expand Up @@ -15,9 +15,9 @@ var p1 = import("./0");

p1.then(zero => {
>p1.then(zero => { return zero.foo();}) : Promise<string>
>p1.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>p1.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>p1 : Promise<typeof import("tests/cases/conformance/dynamicImport/0")>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>zero => { return zero.foo();} : (zero: typeof import("tests/cases/conformance/dynamicImport/0")) => string
>zero : typeof import("tests/cases/conformance/dynamicImport/0")

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/importCallExpression2ES2020.types
Expand Up @@ -14,9 +14,9 @@ function foo(x: Promise<any>) {

x.then(value => {
>x.then(value => { let b = new value.B(); b.print(); }) : Promise<void>
>x.then : <TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>x.then : <TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>x : Promise<any>
>then : <TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>value => { let b = new value.B(); b.print(); } : (value: any) => void
>value : any

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/importCallExpression4ES2020.types
Expand Up @@ -38,11 +38,11 @@ class C {

this.myModule.then(Zero => {
>this.myModule.then(Zero => { console.log(Zero.foo()); }, async err => { console.log(err); let one = await import("./1"); console.log(one.backup()); }) : Promise<void>
>this.myModule.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>this.myModule.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>this.myModule : Promise<typeof import("tests/cases/conformance/dynamicImport/0")>
>this : this
>myModule : Promise<typeof import("tests/cases/conformance/dynamicImport/0")>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>Zero => { console.log(Zero.foo()); } : (Zero: typeof import("tests/cases/conformance/dynamicImport/0")) => void
>Zero : typeof import("tests/cases/conformance/dynamicImport/0")

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/importCallExpressionES5AMD.types
Expand Up @@ -15,9 +15,9 @@ var p1 = import("./0");

p1.then(zero => {
>p1.then(zero => { return zero.foo();}) : Promise<string>
>p1.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>p1.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>p1 : Promise<typeof import("tests/cases/conformance/dynamicImport/0")>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>zero => { return zero.foo();} : (zero: typeof import("tests/cases/conformance/dynamicImport/0")) => string
>zero : typeof import("tests/cases/conformance/dynamicImport/0")

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/importCallExpressionES5CJS.types
Expand Up @@ -15,9 +15,9 @@ var p1 = import("./0");

p1.then(zero => {
>p1.then(zero => { return zero.foo();}) : Promise<string>
>p1.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>p1.then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>p1 : Promise<typeof import("tests/cases/conformance/dynamicImport/0")>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>then : <TResult1 = typeof import("tests/cases/conformance/dynamicImport/0"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/conformance/dynamicImport/0")) => TResult1, onrejected?: (reason: any) => TResult2) => Promise<(TResult1 extends PromiseLike<infer UResult1> ? UResult1 : TResult1) | (TResult2 extends PromiseLike<infer UResult2> ? UResult2 : TResult2)>
>zero => { return zero.foo();} : (zero: typeof import("tests/cases/conformance/dynamicImport/0")) => string
>zero : typeof import("tests/cases/conformance/dynamicImport/0")

Expand Down

0 comments on commit 5535587

Please sign in to comment.