@@ -842,6 +842,7 @@ namespace ts {
842
842
let deferredGlobalESSymbolConstructorSymbol: Symbol | undefined;
843
843
let deferredGlobalESSymbolType: ObjectType;
844
844
let deferredGlobalTypedPropertyDescriptorType: GenericType;
845
+ let deferredGlobalAwaitedSymbol: Symbol | undefined;
845
846
let deferredGlobalPromiseType: GenericType;
846
847
let deferredGlobalPromiseLikeType: GenericType;
847
848
let deferredGlobalPromiseConstructorSymbol: Symbol | undefined;
@@ -896,7 +897,6 @@ namespace ts {
896
897
const potentialThisCollisions: Node[] = [];
897
898
const potentialNewTargetCollisions: Node[] = [];
898
899
const potentialWeakMapCollisions: Node[] = [];
899
- const awaitedTypeStack: number[] = [];
900
900
901
901
const diagnostics = createDiagnosticCollection();
902
902
const suggestionDiagnostics = createDiagnosticCollection();
@@ -11450,6 +11450,10 @@ namespace ts {
11450
11450
return deferredGlobalESSymbolType || (deferredGlobalESSymbolType = getGlobalType("Symbol" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
11451
11451
}
11452
11452
11453
+ function getGlobalAwaitedSymbol(reportErrors: boolean) {
11454
+ return deferredGlobalAwaitedSymbol || (deferredGlobalAwaitedSymbol = getGlobalTypeSymbol("Awaited" as __String, reportErrors));
11455
+ }
11456
+
11453
11457
function getGlobalPromiseType(reportErrors: boolean) {
11454
11458
return deferredGlobalPromiseType || (deferredGlobalPromiseType = getGlobalType("Promise" as __String, /*arity*/ 1, reportErrors)) || emptyGenericType;
11455
11459
}
@@ -26264,8 +26268,6 @@ namespace ts {
26264
26268
// creates a `Promise<T>` type where `T` is the promisedType argument
26265
26269
const globalPromiseType = getGlobalPromiseType(/*reportErrors*/ true);
26266
26270
if (globalPromiseType !== emptyGenericType) {
26267
- // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
26268
- promisedType = getAwaitedType(promisedType) || unknownType;
26269
26271
return createTypeReference(globalPromiseType, [promisedType]);
26270
26272
}
26271
26273
@@ -26276,8 +26278,6 @@ namespace ts {
26276
26278
// creates a `PromiseLike<T>` type where `T` is the promisedType argument
26277
26279
const globalPromiseLikeType = getGlobalPromiseLikeType(/*reportErrors*/ true);
26278
26280
if (globalPromiseLikeType !== emptyGenericType) {
26279
- // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
26280
- promisedType = getAwaitedType(promisedType) || unknownType;
26281
26281
return createTypeReference(globalPromiseLikeType, [promisedType]);
26282
26282
}
26283
26283
@@ -29737,98 +29737,26 @@ namespace ts {
29737
29737
return typeAsAwaitable.awaitedTypeOfType = type;
29738
29738
}
29739
29739
29740
- if (type.flags & TypeFlags.Union) {
29741
- let types: Type[] | undefined;
29742
- for (const constituentType of (<UnionType>type).types) {
29743
- types = append<Type>(types, getAwaitedType(constituentType, errorNode, diagnosticMessage, arg0));
29744
- }
29745
-
29746
- if (!types) {
29747
- return undefined;
29748
- }
29749
-
29750
- return typeAsAwaitable.awaitedTypeOfType = getUnionType(types);
29740
+ const symbol = getGlobalAwaitedSymbol(/*reportErrors*/ false);
29741
+ if (!symbol) {
29742
+ return typeAsAwaitable.awaitedTypeOfType = type;
29751
29743
}
29752
29744
29753
- const promisedType = getPromisedTypeOfPromise(type);
29754
- if (promisedType) {
29755
- if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
29756
- // Verify that we don't have a bad actor in the form of a promise whose
29757
- // promised type is the same as the promise type, or a mutually recursive
29758
- // promise. If so, we return undefined as we cannot guess the shape. If this
29759
- // were the actual case in the JavaScript, this Promise would never resolve.
29760
- //
29761
- // An example of a bad actor with a singly-recursive promise type might
29762
- // be:
29763
- //
29764
- // interface BadPromise {
29765
- // then(
29766
- // onfulfilled: (value: BadPromise) => any,
29767
- // onrejected: (error: any) => any): BadPromise;
29768
- // }
29769
- // The above interface will pass the PromiseLike check, and return a
29770
- // promised type of `BadPromise`. Since this is a self reference, we
29771
- // don't want to keep recursing ad infinitum.
29772
- //
29773
- // An example of a bad actor in the form of a mutually-recursive
29774
- // promise type might be:
29775
- //
29776
- // interface BadPromiseA {
29777
- // then(
29778
- // onfulfilled: (value: BadPromiseB) => any,
29779
- // onrejected: (error: any) => any): BadPromiseB;
29780
- // }
29781
- //
29782
- // interface BadPromiseB {
29783
- // then(
29784
- // onfulfilled: (value: BadPromiseA) => any,
29785
- // onrejected: (error: any) => any): BadPromiseA;
29786
- // }
29787
- //
29788
- if (errorNode) {
29789
- error(errorNode, Diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method);
29790
- }
29791
- return undefined;
29792
- }
29793
-
29794
- // Keep track of the type we're about to unwrap to avoid bad recursive promise types.
29795
- // See the comments above for more information.
29796
- awaitedTypeStack.push(type.id);
29797
- const awaitedType = getAwaitedType(promisedType, errorNode, diagnosticMessage, arg0);
29798
- awaitedTypeStack.pop();
29799
-
29800
- if (!awaitedType) {
29801
- return undefined;
29802
- }
29745
+ if (type.aliasSymbol === symbol) {
29746
+ return typeAsAwaitable.awaitedTypeOfType = type;
29747
+ }
29803
29748
29804
- return typeAsAwaitable.awaitedTypeOfType = awaitedType;
29749
+ const result = getTypeAliasInstantiation(symbol, [type]);
29750
+ if (result !== unknownType || type === unknownType || getPromisedTypeOfPromise(type) === unknownType) {
29751
+ return typeAsAwaitable.awaitedTypeOfType = result;
29805
29752
}
29806
29753
29807
- // The type was not a promise, so it could not be unwrapped any further.
29808
- // As long as the type does not have a callable "then" property, it is
29809
- // safe to return the type; otherwise, an error will be reported in
29810
- // the call to getNonThenableType and we will return undefined.
29811
- //
29812
- // An example of a non-promise "thenable" might be:
29813
- //
29814
- // await { then(): void {} }
29815
- //
29816
- // The "thenable" does not match the minimal definition for a promise. When
29817
- // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise
29818
- // will never settle. We treat this as an error to help flag an early indicator
29819
- // of a runtime problem. If the user wants to return this value from an async
29820
- // function, they would need to wrap it in some other value. If they want it to
29821
- // be treated as a promise, they can cast to <any>.
29822
- const thenFunction = getTypeOfPropertyOfType(type, "then" as __String);
29823
- if (thenFunction && getSignaturesOfType(thenFunction, SignatureKind.Call).length > 0) {
29824
- if (errorNode) {
29825
- if (!diagnosticMessage) return Debug.fail();
29826
- error(errorNode, diagnosticMessage, arg0);
29827
- }
29828
- return undefined;
29754
+ if (errorNode) {
29755
+ if (!diagnosticMessage) return Debug.fail();
29756
+ error(errorNode, diagnosticMessage, arg0);
29829
29757
}
29830
29758
29831
- return typeAsAwaitable.awaitedTypeOfType = type ;
29759
+ return undefined ;
29832
29760
}
29833
29761
29834
29762
/**
0 commit comments