Skip to content

Commit 310836e

Browse files
committed
Add a fastpath for comparing common mapped types like Pick which avoids manufacturing intermediate type identities
1 parent e92afac commit 310836e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16958,12 +16958,23 @@ namespace ts {
1695816958
if (includeOptional
1695916959
? !(filteredByApplicability!.flags & TypeFlags.Never)
1696016960
: isRelatedTo(targetConstraint, sourceKeys)) {
16961-
const typeParameter = getTypeParameterFromMappedType(target);
16962-
const indexingType = filteredByApplicability ? getIntersectionType([filteredByApplicability, typeParameter]) : typeParameter;
16963-
const indexedAccessType = getIndexedAccessType(source, indexingType);
1696416961
const templateType = getTemplateTypeFromMappedType(target);
16965-
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
16966-
return result;
16962+
const typeParameter = getTypeParameterFromMappedType(target);
16963+
16964+
// Fastpath: When the template has the form Obj[P] where P is the mapped type parameter, directly compare `source` with `Obj`
16965+
// to avoid creating the (potentially very large) number of new intermediate types made by manufacturing `source[P]`
16966+
const nonNullComponent = extractTypesOfKind(templateType, ~TypeFlags.Nullable);
16967+
if (nonNullComponent.flags & TypeFlags.IndexedAccess && (nonNullComponent as IndexedAccessType).indexType === typeParameter) {
16968+
if (result = isRelatedTo(source, (nonNullComponent as IndexedAccessType).objectType, reportErrors)) {
16969+
return result;
16970+
}
16971+
}
16972+
else {
16973+
const indexingType = filteredByApplicability ? getIntersectionType([filteredByApplicability, typeParameter]) : typeParameter;
16974+
const indexedAccessType = getIndexedAccessType(source, indexingType);
16975+
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
16976+
return result;
16977+
}
1696716978
}
1696816979
}
1696916980
originalErrorInfo = errorInfo;

0 commit comments

Comments
 (0)