Skip to content

Commit

Permalink
Better typings for Promise executor, like microsoft#31117
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Aug 23, 2019
1 parent 5b59cfb commit 3476da2
Show file tree
Hide file tree
Showing 20 changed files with 311 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/lib/es2015.promise.d.ts
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
27 changes: 26 additions & 1 deletion tests/baselines/reference/asyncArrowFunction10_es5.errors.txt
@@ -1,8 +1,33 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts(1,21): error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
Types of property 'then' are incompatible.
Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts(3,11): error TS2304: Cannot find name 'await'.


==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts (1 errors) ====
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts (2 errors) ====
var foo = async (): Promise<void> => {
~~~~~~~~~~~~~
!!! error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
!!! error TS1055: Types of property 'then' are incompatible.
!!! error TS1055: Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
!!! error TS1055: 'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
!!! error TS1055: 'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
// Legal to use 'await' in a type context.
var v: await;
~~~~~
Expand Down
27 changes: 26 additions & 1 deletion tests/baselines/reference/asyncArrowFunction6_es5.errors.txt
@@ -1,11 +1,36 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts(1,22): error TS2524: 'await' expressions cannot be used in a parameter initializer.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts(1,27): error TS1109: Expression expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts(1,30): error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
Types of property 'then' are incompatible.
Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.


==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts (2 errors) ====
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts (3 errors) ====
var foo = async (a = await): Promise<void> => {
~~~~~
!!! error TS2524: 'await' expressions cannot be used in a parameter initializer.
~
!!! error TS1109: Expression expected.
~~~~~~~~~~~~~
!!! error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
!!! error TS1055: Types of property 'then' are incompatible.
!!! error TS1055: Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
!!! error TS1055: 'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
!!! error TS1055: 'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
}
30 changes: 29 additions & 1 deletion tests/baselines/reference/asyncArrowFunction7_es5.errors.txt
@@ -1,14 +1,42 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts(1,21): error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
Types of property 'then' are incompatible.
Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts(3,24): error TS2524: 'await' expressions cannot be used in a parameter initializer.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts(3,29): error TS1109: Expression expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts(3,32): error TS1055: Type 'PromiseConstructor' 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/asyncArrowFunction/asyncArrowFunction7_es5.ts (2 errors) ====
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts (4 errors) ====
var bar = async (): Promise<void> => {
~~~~~~~~~~~~~
!!! error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
!!! error TS1055: Types of property 'then' are incompatible.
!!! error TS1055: Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
!!! error TS1055: 'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
!!! error TS1055: 'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
// 'await' here is an identifier, and not an await expression.
var foo = async (a = await): Promise<void> => {
~~~~~
!!! error TS2524: 'await' expressions cannot be used in a parameter initializer.
~
!!! error TS1109: Expression expected.
~~~~~~~~~~~~~
!!! error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
}
}
27 changes: 26 additions & 1 deletion tests/baselines/reference/asyncArrowFunction8_es5.errors.txt
@@ -1,8 +1,33 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts(1,21): error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
Types of property 'then' are incompatible.
Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts(2,19): error TS1109: Expression expected.


==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts (1 errors) ====
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts (2 errors) ====
var foo = async (): Promise<void> => {
~~~~~~~~~~~~~
!!! error TS1055: Type 'PromiseConstructor' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
!!! error TS1055: Types of property 'then' are incompatible.
!!! error TS1055: Type '<TResult1 = T | (T extends PromiseLike<infer U> ? U : T), TResult2 = never>(onfulfilled?: (value: T | (T extends PromiseLike<infer U> ? U : T)) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
!!! error TS1055: 'T | (T extends PromiseLike<infer U> ? U : T)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
!!! error TS1055: 'T extends PromiseLike<infer U> ? U : T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
var v = { [await]: foo }
~
!!! error TS1109: Expression expected.
Expand Down

0 comments on commit 3476da2

Please sign in to comment.