diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d75ae6a8cdc..11b36ae80fb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6519,6 +6519,23 @@ namespace ts { function instantiateType(type: Type, mapper: TypeMapper): Type { if (type && mapper !== identityMapper) { + // If we are instantiating a type that has a top-level type alias, obtain the instantiation through + // the type alias instead in order to share instantiations for the same type arguments. This can + // dramatically reduce the number of structurally identical types we generate. Note that we can only + // perform this optimization for top-level type aliases. Consider: + // + // function f1(x: T) { + // type Foo = { x: X, t: T }; + // let obj: Foo = { x: x }; + // return obj; + // } + // function f2(x: U) { return f1(x); } + // let z = f2(42); + // + // Above, the declaration of f2 has an inferred return type that is an instantiation of f1's Foo + // equivalent to { x: U, t: U }. When instantiating this return type, we can't go back to Foo's + // cache because all cached instantiations are of the form { x: ???, t: T }, i.e. they have not been + // instantiated for T. Instead, we need to further instantiate the { x: U, t: U } form. if (type.aliasSymbol && isTopLevelTypeAlias(type.aliasSymbol)) { if (type.aliasTypeArguments) { return getTypeAliasInstantiation(type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));