Skip to content

Commit 1214b11

Browse files
committed
Defer generic awaited type
1 parent 8c5ed7f commit 1214b11

File tree

54 files changed

+505
-567
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+505
-567
lines changed

src/compiler/checker.ts

+18-90
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ namespace ts {
842842
let deferredGlobalESSymbolConstructorSymbol: Symbol | undefined;
843843
let deferredGlobalESSymbolType: ObjectType;
844844
let deferredGlobalTypedPropertyDescriptorType: GenericType;
845+
let deferredGlobalAwaitedSymbol: Symbol | undefined;
845846
let deferredGlobalPromiseType: GenericType;
846847
let deferredGlobalPromiseLikeType: GenericType;
847848
let deferredGlobalPromiseConstructorSymbol: Symbol | undefined;
@@ -896,7 +897,6 @@ namespace ts {
896897
const potentialThisCollisions: Node[] = [];
897898
const potentialNewTargetCollisions: Node[] = [];
898899
const potentialWeakMapCollisions: Node[] = [];
899-
const awaitedTypeStack: number[] = [];
900900

901901
const diagnostics = createDiagnosticCollection();
902902
const suggestionDiagnostics = createDiagnosticCollection();
@@ -11455,6 +11455,10 @@ namespace ts {
1145511455
return deferredGlobalESSymbolType || (deferredGlobalESSymbolType = getGlobalType("Symbol" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
1145611456
}
1145711457

11458+
function getGlobalAwaitedSymbol(reportErrors: boolean) {
11459+
return deferredGlobalAwaitedSymbol || (deferredGlobalAwaitedSymbol = getGlobalTypeSymbol("Awaited" as __String, reportErrors));
11460+
}
11461+
1145811462
function getGlobalPromiseType(reportErrors: boolean) {
1145911463
return deferredGlobalPromiseType || (deferredGlobalPromiseType = getGlobalType("Promise" as __String, /*arity*/ 1, reportErrors)) || emptyGenericType;
1146011464
}
@@ -26272,8 +26276,6 @@ namespace ts {
2627226276
// creates a `Promise<T>` type where `T` is the promisedType argument
2627326277
const globalPromiseType = getGlobalPromiseType(/*reportErrors*/ true);
2627426278
if (globalPromiseType !== emptyGenericType) {
26275-
// if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
26276-
promisedType = getAwaitedType(promisedType) || unknownType;
2627726279
return createTypeReference(globalPromiseType, [promisedType]);
2627826280
}
2627926281

@@ -26284,8 +26286,6 @@ namespace ts {
2628426286
// creates a `PromiseLike<T>` type where `T` is the promisedType argument
2628526287
const globalPromiseLikeType = getGlobalPromiseLikeType(/*reportErrors*/ true);
2628626288
if (globalPromiseLikeType !== emptyGenericType) {
26287-
// if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
26288-
promisedType = getAwaitedType(promisedType) || unknownType;
2628926289
return createTypeReference(globalPromiseLikeType, [promisedType]);
2629026290
}
2629126291

@@ -29746,98 +29746,26 @@ namespace ts {
2974629746
return typeAsAwaitable.awaitedTypeOfType = type;
2974729747
}
2974829748

29749-
if (type.flags & TypeFlags.Union) {
29750-
let types: Type[] | undefined;
29751-
for (const constituentType of (<UnionType>type).types) {
29752-
types = append<Type>(types, getAwaitedType(constituentType, errorNode, diagnosticMessage, arg0));
29753-
}
29754-
29755-
if (!types) {
29756-
return undefined;
29757-
}
29758-
29759-
return typeAsAwaitable.awaitedTypeOfType = getUnionType(types);
29749+
const symbol = getGlobalAwaitedSymbol(/*reportErrors*/ false);
29750+
if (!symbol) {
29751+
return typeAsAwaitable.awaitedTypeOfType = type;
2976029752
}
2976129753

29762-
const promisedType = getPromisedTypeOfPromise(type);
29763-
if (promisedType) {
29764-
if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
29765-
// Verify that we don't have a bad actor in the form of a promise whose
29766-
// promised type is the same as the promise type, or a mutually recursive
29767-
// promise. If so, we return undefined as we cannot guess the shape. If this
29768-
// were the actual case in the JavaScript, this Promise would never resolve.
29769-
//
29770-
// An example of a bad actor with a singly-recursive promise type might
29771-
// be:
29772-
//
29773-
// interface BadPromise {
29774-
// then(
29775-
// onfulfilled: (value: BadPromise) => any,
29776-
// onrejected: (error: any) => any): BadPromise;
29777-
// }
29778-
// The above interface will pass the PromiseLike check, and return a
29779-
// promised type of `BadPromise`. Since this is a self reference, we
29780-
// don't want to keep recursing ad infinitum.
29781-
//
29782-
// An example of a bad actor in the form of a mutually-recursive
29783-
// promise type might be:
29784-
//
29785-
// interface BadPromiseA {
29786-
// then(
29787-
// onfulfilled: (value: BadPromiseB) => any,
29788-
// onrejected: (error: any) => any): BadPromiseB;
29789-
// }
29790-
//
29791-
// interface BadPromiseB {
29792-
// then(
29793-
// onfulfilled: (value: BadPromiseA) => any,
29794-
// onrejected: (error: any) => any): BadPromiseA;
29795-
// }
29796-
//
29797-
if (errorNode) {
29798-
error(errorNode, Diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method);
29799-
}
29800-
return undefined;
29801-
}
29802-
29803-
// Keep track of the type we're about to unwrap to avoid bad recursive promise types.
29804-
// See the comments above for more information.
29805-
awaitedTypeStack.push(type.id);
29806-
const awaitedType = getAwaitedType(promisedType, errorNode, diagnosticMessage, arg0);
29807-
awaitedTypeStack.pop();
29808-
29809-
if (!awaitedType) {
29810-
return undefined;
29811-
}
29754+
if (type.aliasSymbol === symbol) {
29755+
return typeAsAwaitable.awaitedTypeOfType = type;
29756+
}
2981229757

29813-
return typeAsAwaitable.awaitedTypeOfType = awaitedType;
29758+
const result = getTypeAliasInstantiation(symbol, [type]);
29759+
if (result !== unknownType || type === unknownType || getPromisedTypeOfPromise(type) === unknownType) {
29760+
return typeAsAwaitable.awaitedTypeOfType = result;
2981429761
}
2981529762

29816-
// The type was not a promise, so it could not be unwrapped any further.
29817-
// As long as the type does not have a callable "then" property, it is
29818-
// safe to return the type; otherwise, an error will be reported in
29819-
// the call to getNonThenableType and we will return undefined.
29820-
//
29821-
// An example of a non-promise "thenable" might be:
29822-
//
29823-
// await { then(): void {} }
29824-
//
29825-
// The "thenable" does not match the minimal definition for a promise. When
29826-
// a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise
29827-
// will never settle. We treat this as an error to help flag an early indicator
29828-
// of a runtime problem. If the user wants to return this value from an async
29829-
// function, they would need to wrap it in some other value. If they want it to
29830-
// be treated as a promise, they can cast to <any>.
29831-
const thenFunction = getTypeOfPropertyOfType(type, "then" as __String);
29832-
if (thenFunction && getSignaturesOfType(thenFunction, SignatureKind.Call).length > 0) {
29833-
if (errorNode) {
29834-
if (!diagnosticMessage) return Debug.fail();
29835-
error(errorNode, diagnosticMessage, arg0);
29836-
}
29837-
return undefined;
29763+
if (errorNode) {
29764+
if (!diagnosticMessage) return Debug.fail();
29765+
error(errorNode, diagnosticMessage, arg0);
2983829766
}
2983929767

29840-
return typeAsAwaitable.awaitedTypeOfType = type;
29768+
return undefined;
2984129769
}
2984229770

2984329771
/**

src/harness/fourslashInterfaceImpl.ts

+1
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ namespace FourSlashInterface {
961961
typeEntry("PropertyDecorator"),
962962
typeEntry("MethodDecorator"),
963963
typeEntry("ParameterDecorator"),
964+
typeEntry("Awaited"),
964965
typeEntry("PromiseConstructorLike"),
965966
interfaceEntry("PromiseLike"),
966967
interfaceEntry("Promise"),

src/lib/es2015.iterable.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,18 @@ interface PromiseConstructor {
200200
/**
201201
* Creates a Promise that is resolved with an array of results when all of the provided Promises
202202
* resolve, or rejected when any Promise is rejected.
203-
* @param values An array of Promises.
203+
* @param values An iterable of Promises.
204204
* @returns A new Promise.
205205
*/
206-
all<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
206+
all<TAll>(values: Iterable<TAll>): Promise<Awaited<TAll>[]>;
207207

208208
/**
209209
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
210210
* or rejected.
211211
* @param values An iterable of Promises.
212212
* @returns A new Promise.
213213
*/
214-
race<T>(values: Iterable<T>): Promise<T extends PromiseLike<infer U> ? U : T>;
214+
race<T>(values: Iterable<T>): Promise<Awaited<T>>;
215215
}
216216

217217
declare namespace Reflect {

src/lib/es2015.promise.d.ts

+4-76
Original file line numberDiff line numberDiff line change
@@ -18,87 +18,15 @@ interface PromiseConstructor {
1818
* @param values An array of Promises.
1919
* @returns A new Promise.
2020
*/
21-
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
22-
23-
/**
24-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
25-
* resolve, or rejected when any Promise is rejected.
26-
* @param values An array of Promises.
27-
* @returns A new Promise.
28-
*/
29-
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
30-
31-
/**
32-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
33-
* resolve, or rejected when any Promise is rejected.
34-
* @param values An array of Promises.
35-
* @returns A new Promise.
36-
*/
37-
all<T1, T2, T3, T4, T5, T6, T7, T8>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
38-
39-
/**
40-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
41-
* resolve, or rejected when any Promise is rejected.
42-
* @param values An array of Promises.
43-
* @returns A new Promise.
44-
*/
45-
all<T1, T2, T3, T4, T5, T6, T7>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
46-
47-
/**
48-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
49-
* resolve, or rejected when any Promise is rejected.
50-
* @param values An array of Promises.
51-
* @returns A new Promise.
52-
*/
53-
all<T1, T2, T3, T4, T5, T6>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
54-
55-
/**
56-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
57-
* resolve, or rejected when any Promise is rejected.
58-
* @param values An array of Promises.
59-
* @returns A new Promise.
60-
*/
61-
all<T1, T2, T3, T4, T5>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>;
62-
63-
/**
64-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
65-
* resolve, or rejected when any Promise is rejected.
66-
* @param values An array of Promises.
67-
* @returns A new Promise.
68-
*/
69-
all<T1, T2, T3, T4>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): Promise<[T1, T2, T3, T4]>;
70-
71-
/**
72-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
73-
* resolve, or rejected when any Promise is rejected.
74-
* @param values An array of Promises.
75-
* @returns A new Promise.
76-
*/
77-
all<T1, T2, T3>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
78-
79-
/**
80-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
81-
* resolve, or rejected when any Promise is rejected.
82-
* @param values An array of Promises.
83-
* @returns A new Promise.
84-
*/
85-
all<T1, T2>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
86-
87-
/**
88-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
89-
* resolve, or rejected when any Promise is rejected.
90-
* @param values An array of Promises.
91-
* @returns A new Promise.
92-
*/
93-
all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>;
21+
all<T extends readonly any[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>;
9422

9523
/**
9624
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
9725
* or rejected.
9826
* @param values An array of Promises.
9927
* @returns A new Promise.
10028
*/
101-
race<T>(values: readonly T[]): Promise<T extends PromiseLike<infer U> ? U : T>;
29+
race<T extends readonly any[]>(values: T): Promise<Awaited<T[number]>>;
10230

10331
/**
10432
* Creates a new rejected promise for the provided reason.
@@ -112,10 +40,10 @@ interface PromiseConstructor {
11240
* @param value A promise.
11341
* @returns A promise whose internal state matches the provided promise.
11442
*/
115-
resolve<T>(value: T | PromiseLike<T>): Promise<T>;
43+
resolve<T>(value: T): Promise<Awaited<T>>;
11644

11745
/**
118-
* Creates a new resolved promise .
46+
* Creates a new resolved promise.
11947
* @returns A resolved promise.
12048
*/
12149
resolve(): Promise<void>;

src/lib/es5.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,11 @@ declare type PropertyDecorator = (target: Object, propertyKey: string | symbol)
13781378
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
13791379
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
13801380

1381+
// The undefined case is for strictNullChecks false, in which case
1382+
// undefined extends PromiseLike<infer U> is true, which would otherwise
1383+
// make Awaited<undefined> -> unknown.
1384+
type Awaited<T> = T extends undefined ? T : T extends PromiseLike<infer U> ? U : T extends { then: Function } ? unknown : T;
1385+
13811386
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>;
13821387

13831388
interface PromiseLike<T> {

tests/baselines/reference/asyncArrowFunction11_es5.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class A {
1111
await Promise.resolve();
1212
>await Promise.resolve() : void
1313
>Promise.resolve() : Promise<void>
14-
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
14+
>Promise.resolve : { <T>(value: T): Promise<Awaited<T>>; (): Promise<void>; }
1515
>Promise : PromiseConstructor
16-
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
16+
>resolve : { <T>(value: T): Promise<Awaited<T>>; (): Promise<void>; }
1717

1818
const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
1919
>obj : { a: () => this; }

tests/baselines/reference/asyncArrowFunction5_es2017.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es20
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19-
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:144:13: 'Promise' was also declared here.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:72:13: 'Promise' was also declared here.
2020
~
2121
!!! error TS1005: ',' expected.
2222
~~

tests/baselines/reference/asyncArrowFunction5_es5.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19-
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:144:13: 'Promise' was also declared here.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:72:13: 'Promise' was also declared here.
2020
~
2121
!!! error TS1005: ',' expected.
2222
~~

tests/baselines/reference/asyncArrowFunction5_es6.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19-
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:144:13: 'Promise' was also declared here.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:72:13: 'Promise' was also declared here.
2020
~
2121
!!! error TS1005: ',' expected.
2222
~~

tests/baselines/reference/asyncArrowFunction9_es2017.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es20
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19-
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:144:13: 'Promise' was also declared here.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:72:13: 'Promise' was also declared here.
2020
~
2121
!!! error TS1005: ',' expected.
2222
~~

tests/baselines/reference/asyncArrowFunction9_es5.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19-
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:144:13: 'Promise' was also declared here.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:72:13: 'Promise' was also declared here.
2020
~
2121
!!! error TS1005: ',' expected.
2222
~~

0 commit comments

Comments
 (0)