From 3efcfcb70c1070af6bdbb8696c8332c0b265d122 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:12:06 -0700 Subject: [PATCH] Cache getConstraintOfDistributiveConditionalType (#53358) --- 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 cca1bcd1403ff..8d9311a087d26 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13605,6 +13605,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getConstraintOfDistributiveConditionalType(type: ConditionalType): Type | undefined { + if (type.resolvedConstraintOfDistributive !== undefined) { + return type.resolvedConstraintOfDistributive || 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 @@ -13622,10 +13626,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.resolvedConstraintOfDistributive = instantiated; return instantiated; } } } + type.resolvedConstraintOfDistributive = false; return undefined; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index fef447fc2dd50..7e14ff22dede6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6645,6 +6645,8 @@ export interface ConditionalType extends InstantiableType { /** @internal */ resolvedDefaultConstraint?: Type; /** @internal */ + resolvedConstraintOfDistributive?: Type | false; + /** @internal */ mapper?: TypeMapper; /** @internal */ combinedMapper?: TypeMapper;