From 53019064cba5f1fbde6b5d2f80712be2a7e5c4f5 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:22:08 -0700 Subject: [PATCH] Don't recalculate undefined in getConstraintOfDistributiveConditionalType --- src/compiler/checker.ts | 6 ++++++ src/compiler/types.ts | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cc64670dd993c..6e93cf5d1d905 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13594,6 +13594,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getConstraintOfDistributiveConditionalType(type: ConditionalType): Type | undefined { + if (type.resolvedDefaultConstraintOfDistributed !== undefined) { + return type.resolvedDefaultConstraintOfDistributed || undefined; + } + // Check if we have a conditional type of the form 'T extends U ? X : Y', where T is a constrained // type parameter. If so, create an instantiation of the conditional type where T is replaced // with its constraint. We do this because if the constraint is a union type it will be distributed @@ -13611,10 +13615,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (constraint && constraint !== type.checkType) { const instantiated = getConditionalTypeInstantiation(type, prependTypeMapping(type.root.checkType, constraint, type.mapper)); if (!(instantiated.flags & TypeFlags.Never)) { + type.resolvedDefaultConstraintOfDistributed = instantiated; return instantiated; } } } + type.resolvedDefaultConstraintOfDistributed = false; return undefined; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d8234398128ef..016403f36442c 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6630,6 +6630,8 @@ export interface ConditionalType extends InstantiableType { /** @internal */ resolvedDefaultConstraint?: Type; /** @internal */ + resolvedDefaultConstraintOfDistributed?: Type | false; + /** @internal */ mapper?: TypeMapper; /** @internal */ combinedMapper?: TypeMapper;