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 1/2] 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; From c92c51d2a10bfc119630f6214646f9c53a3d72a1 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 27 Mar 2023 13:09:14 -0700 Subject: [PATCH 2/2] Rename to resolvedConstraintOfDistributive --- src/compiler/checker.ts | 8 ++++---- src/compiler/types.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ca3aa287682c9..d87983beae947 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13605,8 +13605,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getConstraintOfDistributiveConditionalType(type: ConditionalType): Type | undefined { - if (type.resolvedDefaultConstraintOfDistributed !== undefined) { - return type.resolvedDefaultConstraintOfDistributed || 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 @@ -13626,12 +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.resolvedDefaultConstraintOfDistributed = instantiated; + type.resolvedConstraintOfDistributive = instantiated; return instantiated; } } } - type.resolvedDefaultConstraintOfDistributed = false; + type.resolvedConstraintOfDistributive = false; return undefined; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 92d4fccdcb45b..7e14ff22dede6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6645,7 +6645,7 @@ export interface ConditionalType extends InstantiableType { /** @internal */ resolvedDefaultConstraint?: Type; /** @internal */ - resolvedDefaultConstraintOfDistributed?: Type | false; + resolvedConstraintOfDistributive?: Type | false; /** @internal */ mapper?: TypeMapper; /** @internal */