Skip to content

Commit 3476da2

Browse files
committed
Better typings for Promise executor, like microsoft#31117
1 parent 5b59cfb commit 3476da2

20 files changed

+311
-37
lines changed

src/lib/es2015.promise.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface PromiseConstructor {
1010
* a resolve callback used to resolve the promise with a value or the result of another promise,
1111
* and a reject callback used to reject the promise with a provided reason or error.
1212
*/
13-
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
13+
new <T>(executor: (resolve: (value?: T) => void, reject: (reason?: any) => void) => void): Promise<T extends PromiseLike<infer U> ? U : T>;
1414

1515
/**
1616
* Creates a Promise that is resolved with an array of results when all of the provided Promises

tests/baselines/reference/asyncArrowFunction10_es5.errors.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1+
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.
2+
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
3+
Types of property 'then' are incompatible.
4+
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>'.
5+
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
6+
Types of parameters 'value' and 'value' are incompatible.
7+
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
8+
'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 '{}'.
9+
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
10+
'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 '{}'.
11+
Type 'unknown' is not assignable to type 'T'.
12+
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
113
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts(3,11): error TS2304: Cannot find name 'await'.
214

315

4-
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts (1 errors) ====
16+
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts (2 errors) ====
517
var foo = async (): Promise<void> => {
18+
~~~~~~~~~~~~~
19+
!!! 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.
20+
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
21+
!!! error TS1055: Types of property 'then' are incompatible.
22+
!!! 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>'.
23+
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
24+
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
25+
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
26+
!!! 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 '{}'.
27+
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
28+
!!! 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 '{}'.
29+
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
30+
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
631
// Legal to use 'await' in a type context.
732
var v: await;
833
~~~~~
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts(1,22): error TS2524: 'await' expressions cannot be used in a parameter initializer.
22
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts(1,27): error TS1109: Expression expected.
3+
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.
4+
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
5+
Types of property 'then' are incompatible.
6+
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>'.
7+
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
8+
Types of parameters 'value' and 'value' are incompatible.
9+
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
10+
'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 '{}'.
11+
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
12+
'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 '{}'.
13+
Type 'unknown' is not assignable to type 'T'.
14+
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
315

416

5-
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts (2 errors) ====
17+
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction6_es5.ts (3 errors) ====
618
var foo = async (a = await): Promise<void> => {
719
~~~~~
820
!!! error TS2524: 'await' expressions cannot be used in a parameter initializer.
921
~
1022
!!! error TS1109: Expression expected.
23+
~~~~~~~~~~~~~
24+
!!! 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.
25+
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
26+
!!! error TS1055: Types of property 'then' are incompatible.
27+
!!! 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>'.
28+
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
29+
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
30+
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
31+
!!! 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 '{}'.
32+
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
33+
!!! 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 '{}'.
34+
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
35+
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
1136
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1+
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.
2+
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
3+
Types of property 'then' are incompatible.
4+
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>'.
5+
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
6+
Types of parameters 'value' and 'value' are incompatible.
7+
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
8+
'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 '{}'.
9+
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
10+
'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 '{}'.
11+
Type 'unknown' is not assignable to type 'T'.
12+
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
113
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts(3,24): error TS2524: 'await' expressions cannot be used in a parameter initializer.
214
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts(3,29): error TS1109: Expression expected.
15+
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.
316

417

5-
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts (2 errors) ====
18+
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction7_es5.ts (4 errors) ====
619
var bar = async (): Promise<void> => {
20+
~~~~~~~~~~~~~
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.
22+
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
23+
!!! error TS1055: Types of property 'then' are incompatible.
24+
!!! 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>'.
25+
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
26+
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
27+
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
28+
!!! 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 '{}'.
29+
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
30+
!!! 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 '{}'.
31+
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
32+
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
733
// 'await' here is an identifier, and not an await expression.
834
var foo = async (a = await): Promise<void> => {
935
~~~~~
1036
!!! error TS2524: 'await' expressions cannot be used in a parameter initializer.
1137
~
1238
!!! error TS1109: Expression expected.
39+
~~~~~~~~~~~~~
40+
!!! 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.
1341
}
1442
}

tests/baselines/reference/asyncArrowFunction8_es5.errors.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1+
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.
2+
Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
3+
Types of property 'then' are incompatible.
4+
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>'.
5+
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
6+
Types of parameters 'value' and 'value' are incompatible.
7+
Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
8+
'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 '{}'.
9+
Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
10+
'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 '{}'.
11+
Type 'unknown' is not assignable to type 'T'.
12+
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
113
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts(2,19): error TS1109: Expression expected.
214

315

4-
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts (1 errors) ====
16+
==== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction8_es5.ts (2 errors) ====
517
var foo = async (): Promise<void> => {
18+
~~~~~~~~~~~~~
19+
!!! 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.
20+
!!! error TS1055: Type 'Promise<T | (T extends PromiseLike<infer U> ? U : T)>' is not assignable to type 'PromiseLike<T>'.
21+
!!! error TS1055: Types of property 'then' are incompatible.
22+
!!! 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>'.
23+
!!! error TS1055: Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
24+
!!! error TS1055: Types of parameters 'value' and 'value' are incompatible.
25+
!!! error TS1055: Type 'T | (T extends PromiseLike<infer U> ? U : T)' is not assignable to type 'T'.
26+
!!! 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 '{}'.
27+
!!! error TS1055: Type 'T extends PromiseLike<infer U> ? U : T' is not assignable to type 'T'.
28+
!!! 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 '{}'.
29+
!!! error TS1055: Type 'unknown' is not assignable to type 'T'.
30+
!!! error TS1055: 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
631
var v = { [await]: foo }
732
~
833
!!! error TS1109: Expression expected.

0 commit comments

Comments
 (0)