@@ -800,6 +800,7 @@ namespace ts {
800800 let deferredGlobalESSymbolConstructorSymbol: Symbol | undefined;
801801 let deferredGlobalESSymbolType: ObjectType;
802802 let deferredGlobalTypedPropertyDescriptorType: GenericType;
803+ let deferredGlobalAwaitedSymbol: Symbol | undefined;
803804 let deferredGlobalPromiseType: GenericType;
804805 let deferredGlobalPromiseLikeType: GenericType;
805806 let deferredGlobalPromiseConstructorSymbol: Symbol | undefined;
@@ -853,7 +854,6 @@ namespace ts {
853854 const flowNodeReachable: (boolean | undefined)[] = [];
854855 const potentialThisCollisions: Node[] = [];
855856 const potentialNewTargetCollisions: Node[] = [];
856- const awaitedTypeStack: number[] = [];
857857
858858 const diagnostics = createDiagnosticCollection();
859859 const suggestionDiagnostics = createDiagnosticCollection();
@@ -11069,6 +11069,10 @@ namespace ts {
1106911069 return deferredGlobalESSymbolType || (deferredGlobalESSymbolType = getGlobalType("Symbol" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
1107011070 }
1107111071
11072+ function getGlobalAwaitedSymbol(reportErrors: boolean) {
11073+ return deferredGlobalAwaitedSymbol || (deferredGlobalAwaitedSymbol = getGlobalTypeSymbol("Awaited" as __String, reportErrors));
11074+ }
11075+
1107211076 function getGlobalPromiseType(reportErrors: boolean) {
1107311077 return deferredGlobalPromiseType || (deferredGlobalPromiseType = getGlobalType("Promise" as __String, /*arity*/ 1, reportErrors)) || emptyGenericType;
1107411078 }
@@ -29012,98 +29016,22 @@ namespace ts {
2901229016 return typeAsAwaitable.awaitedTypeOfType = type;
2901329017 }
2901429018
29015- if (type.flags & TypeFlags.Union) {
29016- let types: Type[] | undefined;
29017- for (const constituentType of (<UnionType>type).types) {
29018- types = append<Type>(types, getAwaitedType(constituentType, errorNode, diagnosticMessage, arg0));
29019- }
29020-
29021- if (!types) {
29022- return undefined;
29023- }
29024-
29025- return typeAsAwaitable.awaitedTypeOfType = getUnionType(types);
29019+ const symbol = getGlobalAwaitedSymbol(/*reportErrors*/ false);
29020+ if (!symbol) {
29021+ return typeAsAwaitable.awaitedTypeOfType = type;
2902629022 }
2902729023
29028- const promisedType = getPromisedTypeOfPromise(type);
29029- if (promisedType) {
29030- if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
29031- // Verify that we don't have a bad actor in the form of a promise whose
29032- // promised type is the same as the promise type, or a mutually recursive
29033- // promise. If so, we return undefined as we cannot guess the shape. If this
29034- // were the actual case in the JavaScript, this Promise would never resolve.
29035- //
29036- // An example of a bad actor with a singly-recursive promise type might
29037- // be:
29038- //
29039- // interface BadPromise {
29040- // then(
29041- // onfulfilled: (value: BadPromise) => any,
29042- // onrejected: (error: any) => any): BadPromise;
29043- // }
29044- // The above interface will pass the PromiseLike check, and return a
29045- // promised type of `BadPromise`. Since this is a self reference, we
29046- // don't want to keep recursing ad infinitum.
29047- //
29048- // An example of a bad actor in the form of a mutually-recursive
29049- // promise type might be:
29050- //
29051- // interface BadPromiseA {
29052- // then(
29053- // onfulfilled: (value: BadPromiseB) => any,
29054- // onrejected: (error: any) => any): BadPromiseB;
29055- // }
29056- //
29057- // interface BadPromiseB {
29058- // then(
29059- // onfulfilled: (value: BadPromiseA) => any,
29060- // onrejected: (error: any) => any): BadPromiseA;
29061- // }
29062- //
29063- if (errorNode) {
29064- error(errorNode, Diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method);
29065- }
29066- return undefined;
29067- }
29068-
29069- // Keep track of the type we're about to unwrap to avoid bad recursive promise types.
29070- // See the comments above for more information.
29071- awaitedTypeStack.push(type.id);
29072- const awaitedType = getAwaitedType(promisedType, errorNode, diagnosticMessage, arg0);
29073- awaitedTypeStack.pop();
29074-
29075- if (!awaitedType) {
29076- return undefined;
29077- }
29078-
29079- return typeAsAwaitable.awaitedTypeOfType = awaitedType;
29024+ const result = getTypeAliasInstantiation(symbol, [type]);
29025+ if (result !== unknownType || type === unknownType || getPromisedTypeOfPromise(type) === unknownType) {
29026+ return typeAsAwaitable.awaitedTypeOfType = (result as PromiseOrAwaitableType).awaitedTypeOfType = result;
2908029027 }
2908129028
29082- // The type was not a promise, so it could not be unwrapped any further.
29083- // As long as the type does not have a callable "then" property, it is
29084- // safe to return the type; otherwise, an error will be reported in
29085- // the call to getNonThenableType and we will return undefined.
29086- //
29087- // An example of a non-promise "thenable" might be:
29088- //
29089- // await { then(): void {} }
29090- //
29091- // The "thenable" does not match the minimal definition for a promise. When
29092- // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise
29093- // will never settle. We treat this as an error to help flag an early indicator
29094- // of a runtime problem. If the user wants to return this value from an async
29095- // function, they would need to wrap it in some other value. If they want it to
29096- // be treated as a promise, they can cast to <any>.
29097- const thenFunction = getTypeOfPropertyOfType(type, "then" as __String);
29098- if (thenFunction && getSignaturesOfType(thenFunction, SignatureKind.Call).length > 0) {
29099- if (errorNode) {
29100- if (!diagnosticMessage) return Debug.fail();
29101- error(errorNode, diagnosticMessage, arg0);
29102- }
29103- return undefined;
29029+ if (errorNode) {
29030+ if (!diagnosticMessage) return Debug.fail();
29031+ error(errorNode, diagnosticMessage, arg0);
2910429032 }
2910529033
29106- return typeAsAwaitable.awaitedTypeOfType = type ;
29034+ return undefined ;
2910729035 }
2910829036
2910929037 /**
0 commit comments