Skip to content

Commit

Permalink
Don't recalculate undefined in getConstraintOfDistributiveConditional…
Browse files Browse the repository at this point in the history
…Type
  • Loading branch information
jakebailey committed Mar 19, 2023
1 parent b70784e commit 5301906
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6630,6 +6630,8 @@ export interface ConditionalType extends InstantiableType {
/** @internal */
resolvedDefaultConstraint?: Type;
/** @internal */
resolvedDefaultConstraintOfDistributed?: Type | false;
/** @internal */
mapper?: TypeMapper;
/** @internal */
combinedMapper?: TypeMapper;
Expand Down

0 comments on commit 5301906

Please sign in to comment.