@@ -36601,6 +36601,37 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
3660136601 type;
3660236602 }
3660336603
36604+ function isAwaitedTypeNeeded(type: Type) {
36605+ // If this is already an `Awaited<T>`, we shouldn't wrap it. This helps to avoid `Awaited<Awaited<T>>` in higher-order.
36606+ if (isTypeAny(type) || isAwaitedTypeInstantiation(type)) {
36607+ return false;
36608+ }
36609+
36610+ // We only need `Awaited<T>` if `T` contains possibly non-primitive types.
36611+ if (isGenericObjectType(type)) {
36612+ const baseConstraint = getBaseConstraintOfType(type);
36613+ // We only need `Awaited<T>` if `T` has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
36614+ // or is promise-like.
36615+ if (!baseConstraint || (baseConstraint.flags & TypeFlags.AnyOrUnknown) || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint)) {
36616+ return true;
36617+ }
36618+ }
36619+
36620+ return false;
36621+ }
36622+
36623+ function tryCreateAwaitedType(type: Type): Type | undefined {
36624+ // Nothing to do if `Awaited<T>` doesn't exist
36625+ const awaitedSymbol = getGlobalAwaitedSymbol(/*reportErrors*/ true);
36626+ if (awaitedSymbol) {
36627+ // Unwrap unions that may contain `Awaited<T>`, otherwise its possible to manufacture an `Awaited<Awaited<T> | U>` where
36628+ // an `Awaited<T | U>` would suffice.
36629+ return getTypeAliasInstantiation(awaitedSymbol, [unwrapAwaitedType(type)]);
36630+ }
36631+
36632+ return undefined;
36633+ }
36634+
3660436635 function createAwaitedTypeIfNeeded(type: Type): Type {
3660536636 // We wrap type `T` in `Awaited<T>` based on the following conditions:
3660636637 // - `T` is not already an `Awaited<U>`, and
@@ -36610,28 +36641,10 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
3661036641 // - The base constraint of `T` is `any`, `unknown`, `object`, or `{}`, or
3661136642 // - The base constraint of `T` is an object type with a callable `then` method.
3661236643
36613- if (isTypeAny(type)) {
36614- return type;
36615- }
36616-
36617- // If this is already an `Awaited<T>`, just return it. This helps to avoid `Awaited<Awaited<T>>` in higher-order.
36618- if (isAwaitedTypeInstantiation(type)) {
36619- return type;
36620- }
36621-
36622- // Only instantiate `Awaited<T>` if `T` contains possibly non-primitive types.
36623- if (isGenericObjectType(type)) {
36624- const baseConstraint = getBaseConstraintOfType(type);
36625- // Only instantiate `Awaited<T>` if `T` has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
36626- // or is promise-like.
36627- if (!baseConstraint || (baseConstraint.flags & TypeFlags.AnyOrUnknown) || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint)) {
36628- // Nothing to do if `Awaited<T>` doesn't exist
36629- const awaitedSymbol = getGlobalAwaitedSymbol(/*reportErrors*/ true);
36630- if (awaitedSymbol) {
36631- // Unwrap unions that may contain `Awaited<T>`, otherwise its possible to manufacture an `Awaited<Awaited<T> | U>` where
36632- // an `Awaited<T | U>` would suffice.
36633- return getTypeAliasInstantiation(awaitedSymbol, [unwrapAwaitedType(type)]);
36634- }
36644+ if (isAwaitedTypeNeeded(type)) {
36645+ const awaitedType = tryCreateAwaitedType(type);
36646+ if (awaitedType) {
36647+ return awaitedType;
3663536648 }
3663636649 }
3663736650
@@ -36693,6 +36706,11 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
3669336706 return typeAsAwaitable.awaitedTypeOfType = mapped;
3669436707 }
3669536708
36709+ // If `type` is generic and should be wrapped in `Awaited<T>`, return it.
36710+ if (isAwaitedTypeNeeded(type)) {
36711+ return typeAsAwaitable.awaitedTypeOfType = type;
36712+ }
36713+
3669636714 const thisTypeForErrorOut: { value: Type | undefined } = { value: undefined };
3669736715 const promisedType = getPromisedTypeOfPromise(type, /*errorNode*/ undefined, thisTypeForErrorOut);
3669836716 if (promisedType) {
0 commit comments