@@ -799,6 +799,7 @@ namespace ts {
799799 let deferredGlobalESSymbolConstructorSymbol: Symbol | undefined;
800800 let deferredGlobalESSymbolType: ObjectType;
801801 let deferredGlobalTypedPropertyDescriptorType: GenericType;
802+ let deferredGlobalAwaitedSymbol: Symbol | undefined;
802803 let deferredGlobalPromiseType: GenericType;
803804 let deferredGlobalPromiseLikeType: GenericType;
804805 let deferredGlobalPromiseConstructorSymbol: Symbol | undefined;
@@ -852,7 +853,6 @@ namespace ts {
852853 const flowNodeReachable: (boolean | undefined)[] = [];
853854 const potentialThisCollisions: Node[] = [];
854855 const potentialNewTargetCollisions: Node[] = [];
855- const awaitedTypeStack: number[] = [];
856856
857857 const diagnostics = createDiagnosticCollection();
858858 const suggestionDiagnostics = createDiagnosticCollection();
@@ -11121,6 +11121,10 @@ namespace ts {
1112111121 return deferredGlobalESSymbolType || (deferredGlobalESSymbolType = getGlobalType("Symbol" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
1112211122 }
1112311123
11124+ function getGlobalAwaitedSymbol(reportErrors: boolean) {
11125+ return deferredGlobalAwaitedSymbol || (deferredGlobalAwaitedSymbol = getGlobalTypeSymbol("Awaited" as __String, reportErrors));
11126+ }
11127+
1112411128 function getGlobalPromiseType(reportErrors: boolean) {
1112511129 return deferredGlobalPromiseType || (deferredGlobalPromiseType = getGlobalType("Promise" as __String, /*arity*/ 1, reportErrors)) || emptyGenericType;
1112611130 }
@@ -29100,98 +29104,22 @@ namespace ts {
2910029104 return typeAsAwaitable.awaitedTypeOfType = type;
2910129105 }
2910229106
29103- if (type.flags & TypeFlags.Union) {
29104- let types: Type[] | undefined;
29105- for (const constituentType of (<UnionType>type).types) {
29106- types = append<Type>(types, getAwaitedType(constituentType, errorNode, diagnosticMessage, arg0));
29107- }
29108-
29109- if (!types) {
29110- return undefined;
29111- }
29112-
29113- return typeAsAwaitable.awaitedTypeOfType = getUnionType(types);
29107+ const symbol = getGlobalAwaitedSymbol(/*reportErrors*/ false);
29108+ if (!symbol) {
29109+ return typeAsAwaitable.awaitedTypeOfType = type;
2911429110 }
2911529111
29116- const promisedType = getPromisedTypeOfPromise(type);
29117- if (promisedType) {
29118- if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
29119- // Verify that we don't have a bad actor in the form of a promise whose
29120- // promised type is the same as the promise type, or a mutually recursive
29121- // promise. If so, we return undefined as we cannot guess the shape. If this
29122- // were the actual case in the JavaScript, this Promise would never resolve.
29123- //
29124- // An example of a bad actor with a singly-recursive promise type might
29125- // be:
29126- //
29127- // interface BadPromise {
29128- // then(
29129- // onfulfilled: (value: BadPromise) => any,
29130- // onrejected: (error: any) => any): BadPromise;
29131- // }
29132- // The above interface will pass the PromiseLike check, and return a
29133- // promised type of `BadPromise`. Since this is a self reference, we
29134- // don't want to keep recursing ad infinitum.
29135- //
29136- // An example of a bad actor in the form of a mutually-recursive
29137- // promise type might be:
29138- //
29139- // interface BadPromiseA {
29140- // then(
29141- // onfulfilled: (value: BadPromiseB) => any,
29142- // onrejected: (error: any) => any): BadPromiseB;
29143- // }
29144- //
29145- // interface BadPromiseB {
29146- // then(
29147- // onfulfilled: (value: BadPromiseA) => any,
29148- // onrejected: (error: any) => any): BadPromiseA;
29149- // }
29150- //
29151- if (errorNode) {
29152- error(errorNode, Diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method);
29153- }
29154- return undefined;
29155- }
29156-
29157- // Keep track of the type we're about to unwrap to avoid bad recursive promise types.
29158- // See the comments above for more information.
29159- awaitedTypeStack.push(type.id);
29160- const awaitedType = getAwaitedType(promisedType, errorNode, diagnosticMessage, arg0);
29161- awaitedTypeStack.pop();
29162-
29163- if (!awaitedType) {
29164- return undefined;
29165- }
29166-
29167- return typeAsAwaitable.awaitedTypeOfType = awaitedType;
29112+ const result = getTypeAliasInstantiation(symbol, [type]);
29113+ if (result !== unknownType || type === unknownType || getPromisedTypeOfPromise(type) === unknownType) {
29114+ return typeAsAwaitable.awaitedTypeOfType = (result as PromiseOrAwaitableType).awaitedTypeOfType = result;
2916829115 }
2916929116
29170- // The type was not a promise, so it could not be unwrapped any further.
29171- // As long as the type does not have a callable "then" property, it is
29172- // safe to return the type; otherwise, an error will be reported in
29173- // the call to getNonThenableType and we will return undefined.
29174- //
29175- // An example of a non-promise "thenable" might be:
29176- //
29177- // await { then(): void {} }
29178- //
29179- // The "thenable" does not match the minimal definition for a promise. When
29180- // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise
29181- // will never settle. We treat this as an error to help flag an early indicator
29182- // of a runtime problem. If the user wants to return this value from an async
29183- // function, they would need to wrap it in some other value. If they want it to
29184- // be treated as a promise, they can cast to <any>.
29185- const thenFunction = getTypeOfPropertyOfType(type, "then" as __String);
29186- if (thenFunction && getSignaturesOfType(thenFunction, SignatureKind.Call).length > 0) {
29187- if (errorNode) {
29188- if (!diagnosticMessage) return Debug.fail();
29189- error(errorNode, diagnosticMessage, arg0);
29190- }
29191- return undefined;
29117+ if (errorNode) {
29118+ if (!diagnosticMessage) return Debug.fail();
29119+ error(errorNode, diagnosticMessage, arg0);
2919229120 }
2919329121
29194- return typeAsAwaitable.awaitedTypeOfType = type ;
29122+ return undefined ;
2919529123 }
2919629124
2919729125 /**
0 commit comments