Skip to content

Add a fastpath for comparing common mapped types like Pick which avoids manufacturing intermediate type identities #39696

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

Merged
Merged
Changes from all commits
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
21 changes: 16 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16958,12 +16958,23 @@ namespace ts {
if (includeOptional
? !(filteredByApplicability!.flags & TypeFlags.Never)
: isRelatedTo(targetConstraint, sourceKeys)) {
const typeParameter = getTypeParameterFromMappedType(target);
const indexingType = filteredByApplicability ? getIntersectionType([filteredByApplicability, typeParameter]) : typeParameter;
const indexedAccessType = getIndexedAccessType(source, indexingType);
const templateType = getTemplateTypeFromMappedType(target);
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
return result;
const typeParameter = getTypeParameterFromMappedType(target);

// Fastpath: When the template has the form Obj[P] where P is the mapped type parameter, directly compare `source` with `Obj`
// to avoid creating the (potentially very large) number of new intermediate types made by manufacturing `source[P]`
const nonNullComponent = extractTypesOfKind(templateType, ~TypeFlags.Nullable);
if (nonNullComponent.flags & TypeFlags.IndexedAccess && (nonNullComponent as IndexedAccessType).indexType === typeParameter) {
if (result = isRelatedTo(source, (nonNullComponent as IndexedAccessType).objectType, reportErrors)) {
return result;
}
}
else {
const indexingType = filteredByApplicability ? getIntersectionType([filteredByApplicability, typeParameter]) : typeParameter;
const indexedAccessType = getIndexedAccessType(source, indexingType);
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
return result;
}
}
}
originalErrorInfo = errorInfo;
Expand Down