Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache getConstraintOfDistributiveConditionalType #53358

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolvedDefaultConstraintOfDistributed?: Type | false;
resolvedDefaultConstraintOfDistributive?: Type | false;

it'd be weird to not match the function name, y'know. Plus, a distributive conditional we're getting the distributive constraint of can't have been distributed yet, since, if it was, we wouldn't have a distributive conditional anymore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, then maybe this should really just be resolvedConstraintOfDistributive to match the function? I think I brainfarted when copying the declaration above to rename it and just kept the word "default" without thinking about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to the straight substring of the function name, which isn't exactly what you said but I think is actually correct.

/** @internal */
mapper?: TypeMapper;
/** @internal */
combinedMapper?: TypeMapper;
Expand Down