Skip to content

Commit

Permalink
Better typings for Promise executor, like #31117
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Nov 16, 2019
1 parent 196c0aa commit 874a182
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 23 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 @@ -10,7 +10,7 @@ interface PromiseConstructor {
* a resolve callback used to resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
new <T>(executor: (resolve: (value?: T) => void, reject: (reason?: any) => void) => void): Promise<T extends PromiseLike<infer U> ? U : T>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
Expand Down
2 changes: 1 addition & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ 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>;
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T) => void, reject: (reason?: any) => void) => void) => PromiseLike<T extends PromiseLike<infer U> ? U : T>;

interface PromiseLike<T> {
/**
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/asyncAwaitNestedClasses_es5.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class A {
return new Promise((resolve) => { resolve(null); });
>new Promise((resolve) => { resolve(null); }) : Promise<void>
>Promise : PromiseConstructor
>(resolve) => { resolve(null); } : (resolve: (value?: void | PromiseLike<void>) => void) => void
>resolve : (value?: void | PromiseLike<void>) => void
>(resolve) => { resolve(null); } : (resolve: (value?: void) => void) => void
>resolve : (value?: void) => void
>resolve(null) : void
>resolve : (value?: void | PromiseLike<void>) => void
>resolve : (value?: void) => void
>null : null
}
static C = class C {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(9,23): error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
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.
Construct signature return types 'Thenable' and 'PromiseLike<T extends PromiseLike<infer U> ? U : T>' are incompatible.
The types returned by 'then(...)' are incompatible between these types.
Type 'void' is not assignable to type 'PromiseLike<TResult1 | 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.
Expand Down Expand Up @@ -37,7 +37,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
async function fn6(): Thenable { } // error
~~~~~~~~
!!! 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: Construct signature return types 'Thenable' and 'PromiseLike<T extends PromiseLike<infer U> ? U : 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>'.
async function fn7() { return; } // valid: Promise<void>
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/asyncImportedPromise_es5.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/async/es5/task.ts ===
export class Task<T> extends Promise<T> { }
>Task : Task<T>
>Promise : Promise<T>
>Promise : Promise<T extends PromiseLike<infer U> ? U : T>

=== tests/cases/conformance/async/es5/test.ts ===
import { Task } from "./task";
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/asyncImportedPromise_es6.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/async/es6/task.ts ===
export class Task<T> extends Promise<T> { }
>Task : Task<T>
>Promise : Promise<T>
>Promise : Promise<T extends PromiseLike<infer U> ? U : T>

=== tests/cases/conformance/async/es6/test.ts ===
import { Task } from "./task";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace X {

export class MyPromise<T> extends Promise<T> {
>MyPromise : MyPromise<T>
>Promise : Promise<T>
>Promise : Promise<T extends PromiseLike<infer U> ? U : T>
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace X {

export class MyPromise<T> extends Promise<T> {
>MyPromise : MyPromise<T>
>Promise : Promise<T>
>Promise : Promise<T extends PromiseLike<infer U> ? U : T>
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ async function fn1(): Promise<Obj> {
>await new Promise(resolve => resolve({ key: "value" })) : Obj
>new Promise(resolve => resolve({ key: "value" })) : Promise<Obj>
>Promise : PromiseConstructor
>resolve => resolve({ key: "value" }) : (resolve: (value?: Obj | PromiseLike<Obj>) => void) => void
>resolve : (value?: Obj | PromiseLike<Obj>) => void
>resolve => resolve({ key: "value" }) : (resolve: (value?: Obj) => void) => void
>resolve : (value?: Obj) => void
>resolve({ key: "value" }) : void
>resolve : (value?: Obj | PromiseLike<Obj>) => void
>resolve : (value?: Obj) => void
>{ key: "value" } : { key: "value"; }
>key : "value"
>"value" : "value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ async function fn2(): Promise<Obj> {
return new Promise(resolve => {
>new Promise(resolve => { resolve({ key: "value" }); }) : Promise<Obj>
>Promise : PromiseConstructor
>resolve => { resolve({ key: "value" }); } : (resolve: (value?: Obj | PromiseLike<Obj>) => void) => void
>resolve : (value?: Obj | PromiseLike<Obj>) => void
>resolve => { resolve({ key: "value" }); } : (resolve: (value?: Obj) => void) => void
>resolve : (value?: Obj) => void

resolve({ key: "value" });
>resolve({ key: "value" }) : void
>resolve : (value?: Obj | PromiseLike<Obj>) => void
>resolve : (value?: Obj) => void
>{ key: "value" } : { key: "value"; }
>key : "value"
>"value" : "value"
Expand All @@ -47,12 +47,12 @@ async function fn4(): Promise<Obj> {
>await new Promise(resolve => { resolve({ key: "value" }); }) : Obj
>new Promise(resolve => { resolve({ key: "value" }); }) : Promise<Obj>
>Promise : PromiseConstructor
>resolve => { resolve({ key: "value" }); } : (resolve: (value?: Obj | PromiseLike<Obj>) => void) => void
>resolve : (value?: Obj | PromiseLike<Obj>) => void
>resolve => { resolve({ key: "value" }); } : (resolve: (value?: Obj) => void) => void
>resolve : (value?: Obj) => void

resolve({ key: "value" });
>resolve({ key: "value" }) : void
>resolve : (value?: Obj | PromiseLike<Obj>) => void
>resolve : (value?: Obj) => void
>{ key: "value" } : { key: "value"; }
>key : "value"
>"value" : "value"
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/inferenceLimit.types
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class BrokenClass {
>new Promise<Array<MyModule.MyModel>>((resolve, reject) => { let result: Array<MyModule.MyModel> = []; let populateItems = (order) => { return new Promise((resolve, reject) => { this.doStuff(order.id) .then((items) => { order.items = items; resolve(order); }); }); }; return Promise.all(result.map(populateItems)) .then((orders: Array<MyModule.MyModel>) => { resolve(orders); }); }) : Promise<MyModule.MyModel[]>
>Promise : PromiseConstructor
>MyModule : any
>(resolve, reject) => { let result: Array<MyModule.MyModel> = []; let populateItems = (order) => { return new Promise((resolve, reject) => { this.doStuff(order.id) .then((items) => { order.items = items; resolve(order); }); }); }; return Promise.all(result.map(populateItems)) .then((orders: Array<MyModule.MyModel>) => { resolve(orders); }); } : (resolve: (value?: MyModule.MyModel[] | PromiseLike<MyModule.MyModel[]>) => void, reject: (reason?: any) => void) => Promise<void>
>resolve : (value?: MyModule.MyModel[] | PromiseLike<MyModule.MyModel[]>) => void
>(resolve, reject) => { let result: Array<MyModule.MyModel> = []; let populateItems = (order) => { return new Promise((resolve, reject) => { this.doStuff(order.id) .then((items) => { order.items = items; resolve(order); }); }); }; return Promise.all(result.map(populateItems)) .then((orders: Array<MyModule.MyModel>) => { resolve(orders); }); } : (resolve: (value?: MyModule.MyModel[]) => void, reject: (reason?: any) => void) => Promise<void>
>resolve : (value?: MyModule.MyModel[]) => void
>reject : (reason?: any) => void

let result: Array<MyModule.MyModel> = [];
Expand Down Expand Up @@ -93,7 +93,7 @@ export class BrokenClass {

resolve(orders);
>resolve(orders) : void
>resolve : (value?: MyModule.MyModel[] | PromiseLike<MyModule.MyModel[]>) => void
>resolve : (value?: MyModule.MyModel[]) => void
>orders : MyModule.MyModel[]

});
Expand Down

0 comments on commit 874a182

Please sign in to comment.