Skip to content

Commit c1f2aba

Browse files
authored
Cache substitution types (#30775)
1 parent 8684c3b commit c1f2aba

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ namespace ts {
392392
const literalTypes = createMap<LiteralType>();
393393
const indexedAccessTypes = createMap<IndexedAccessType>();
394394
const conditionalTypes = createMap<Type>();
395+
const substitutionTypes = createMap<SubstitutionType>();
395396
const evolvingArrayTypes: EvolvingArrayType[] = [];
396397
const undefinedProperties = createMap<Symbol>() as UnderscoreEscapedMap<Symbol>;
397398

@@ -8914,9 +8915,15 @@ namespace ts {
89148915
if (substitute.flags & TypeFlags.AnyOrUnknown) {
89158916
return typeVariable;
89168917
}
8918+
const id = `${getTypeId(typeVariable)}>${getTypeId(substitute)}`;
8919+
const cached = substitutionTypes.get(id);
8920+
if (cached) {
8921+
return cached;
8922+
}
89178923
const result = <SubstitutionType>createType(TypeFlags.Substitution);
89188924
result.typeVariable = typeVariable;
89198925
result.substitute = substitute;
8926+
substitutionTypes.set(id, result);
89208927
return result;
89218928
}
89228929

tests/baselines/reference/inlinedAliasAssignableToConstraintSameAsAlias.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class A {
3131
>z : A[]
3232

3333
whereRelated< // Works // Type is same as A1, but is not assignable to type A
34-
>whereRelated : <RF extends RelationFields = RelationFields, N extends "x" | "y" | "z" = "x" | "y" | "z", A1 extends A = RF[N] extends A[] ? RF[N][0] : never, A2 extends A = ShouldA<RF, N>>() => number
34+
>whereRelated : <RF extends RelationFields = RelationFields, N extends "x" | "y" | "z" = "x" | "y" | "z", A1 extends A = RF[N] extends A[] ? RF[N][0] : never, A2 extends A = RF[N] extends A[] ? RF[N][0] : never>() => number
3535

3636
RF extends RelationFields = RelationFields,
3737
N extends Name = Name,

0 commit comments

Comments
 (0)