Skip to content

Commit 236b83f

Browse files
committed
Replace replaceable indexed accesses early to reuse cache entries
1 parent 5026c66 commit 236b83f

3 files changed

Lines changed: 14 additions & 31 deletions

File tree

src/compiler/checker.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14663,23 +14663,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1466314663
inferredProp.declarations = prop.declarations;
1466414664
inferredProp.links.nameType = getSymbolLinks(prop).nameType;
1466514665
inferredProp.links.propertyType = getTypeOfSymbol(prop);
14666-
if (
14667-
type.constraintType.type.flags & TypeFlags.IndexedAccess
14668-
&& (type.constraintType.type as IndexedAccessType).objectType.flags & TypeFlags.TypeParameter
14669-
&& (type.constraintType.type as IndexedAccessType).indexType.flags & TypeFlags.TypeParameter
14670-
) {
14671-
// A reverse mapping of `{[K in keyof T[K_1]]: T[K_1]}` is the same as that of `{[K in keyof T]: T}`, since all we care about is
14672-
// inferring to the "type parameter" (or indexed access) shared by the constraint and template. So, to reduce the number of
14673-
// type identities produced, we simplify such indexed access occurences
14674-
const newTypeParam = (type.constraintType.type as IndexedAccessType).objectType;
14675-
const newMappedType = replaceIndexedAccess(type.mappedType, type.constraintType.type as ReplaceableIndexedAccessType, newTypeParam);
14676-
inferredProp.links.mappedType = newMappedType as MappedType;
14677-
inferredProp.links.constraintType = getIndexType(newTypeParam) as IndexType;
14678-
}
14679-
else {
14680-
inferredProp.links.mappedType = type.mappedType;
14681-
inferredProp.links.constraintType = type.constraintType;
14682-
}
14666+
inferredProp.links.mappedType = type.mappedType;
14667+
inferredProp.links.constraintType = type.constraintType;
1468314668
members.set(prop.escapedName, inferredProp);
1468414669
}
1468514670
setStructuredTypeMembers(type, members, emptyArray, emptyArray, indexInfos);
@@ -26370,6 +26355,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2637026355
* variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
2637126356
*/
2637226357
function inferTypeForHomomorphicMappedType(source: Type, target: MappedType, constraint: IndexType): Type | undefined {
26358+
// A reverse mapping of `{[K in keyof T[K_1]]: T[K_1]}` is the same as that of `{[K in keyof T]: T}`, since all we care about is
26359+
// inferring to the "type parameter" (or indexed access) shared by the constraint and template. So, to reduce the number of
26360+
// type identities produced, we simplify such indexed access occurences
26361+
if (constraint.type.flags & TypeFlags.IndexedAccess) {
26362+
const newTypeParam = (constraint.type as IndexedAccessType).objectType;
26363+
target = replaceIndexedAccess(target, constraint.type as ReplaceableIndexedAccessType, newTypeParam) as MappedType;
26364+
constraint = getIndexType(newTypeParam) as IndexType;
26365+
}
2637326366
const cacheKey = source.id + "," + target.id + "," + constraint.id;
2637426367
if (reverseHomomorphicMappedCache.has(cacheKey)) {
2637526368
return reverseHomomorphicMappedCache.get(cacheKey);

0 commit comments

Comments
 (0)