Repository navigation
Cache homomorphic mapped type constituent instantiations under their re-instantiation key - #65562
Conversation
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
…re-instantiation key Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
… another of its own type parameters Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
|
TypeScript Bot (@typescript-bot) test it |
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to shared compiler type-identity caching and recursive mapper behavior require maintainer review and full compiler/conformance baseline validation unavailable in this environment.
No open issues 🎉
What changed in this PR
Addresses #65560 by caching homomorphic mapped-type constituents under their re-instantiation keys, preventing duplicate union members, quadratic inference work, and spurious TS2859 errors.
Changes:
- Reuses cached constituent instantiations while preserving alias handling.
- Skips caching when overlapping type-parameter mappings would produce an inconsistent cache entry.
- Adds regression tests for aliases, union arguments, the 4,000-key reproduction, and recursive overlapping parameters.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.ts |
Tests recursive mappings with overlapping parameters. |
tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationLargeUnion.ts |
Covers the 4,000-key TS2859 reproduction. |
tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts |
Tests deduplication across mapped types, unions, and aliases. |
tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.types |
Records expected recursive mapping types. |
tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.symbols |
Records overlapping-parameter symbol bindings. |
tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.types |
Records deduplicated inferred types. |
tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.symbols |
Records symbol bindings for deduplication tests. |
tsc/internal/checker/checker.go |
Adds guarded caching for mapped-type constituent instantiations. |
Review effort: Balanced
|
Hey Ryan Cavanaugh (@RyanCavanaugh), the results of running the DT tests are ready. Everything looks the same! |
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the user tests with tsc comparing Everything looks good! |
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
|
Ryan Cavanaugh (@RyanCavanaugh), the perf run you requested failed. You can check the log here. |
|
TypeScript Bot (@typescript-bot) perf test this faster |
|
Okay but why weren't the types reduced on union construction? |
|
Ryan Cavanaugh (@RyanCavanaugh) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This needs a merge from main and baseline reaccept |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e5f9f6f-490e-46f9-bc3a-de0bb1d6728f
Regenerate compiler baselines for the homomorphic mapped type cache change. Validation: npx hereby validate --all; compiler baselines, API tests, lint, and formatting passed. Two Windows symlink-watcher tests failed because this process lacks symlink privileges. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e5f9f6f-490e-46f9-bc3a-de0bb1d6728f
An aliased homomorphic mapped type such as
type Options = Partial<{ verbose: boolean }>gets a structurally identical duplicate each time it is instantiated again. This shows up as repeated union members. It also makes inference quadratic (inferFromMatchingTypesfalls back toisTypeIdenticalTo) and causes spurious TS2859 errors.Root cause
getObjectTypeInstantiationcaches under(outer type args, alias). Here that key is([{verbose}], Options).instantiateMappedTypebuilds each constituent withinstantiateAnonymousTypedirectly, so the constituent is never cached. It gets the aliasPartial<{verbose}>.([{verbose}], Partial<{verbose}>). Nothing is stored there, so a duplicate is created.Partial<A | B>) and for separate aliases of one instantiation (type A1 = Partial<N>; type A2 = Partial<N>).Change (
checker.go)getMappedTypeConstituentInstantiationstores each constituent in the mapped type'sinstantiationsmap. The key is(mapper applied to the outer type parameters, instantiateTypeAlias(t.alias, mapper)), which is exactly the key re-instantiation computes. Printed types are unchanged.prependTypeMapping(T, s, m).Map(T) != s.sis itself one of the mapped type's type parameters andmmaps it again. The merged mapper then sendsTtom.Map(s).type F<T, U> = { [K in keyof T]: F<U, string> }could makeF<string, string>an object type instead ofstring.Tests
homomorphicMappedTypeReinstantiation:Partial,Readonly, a custom homomorphic type, union arguments (with and without an alias), and theA1/A2aliases.homomorphicMappedTypeReinstantiationLargeUnion: the issue's 4,000-key repro (no errors expected).homomorphicMappedTypeReinstantiationOverlappingParameters: self-referential types with overlapping type parameters, in primitive, union and array forms. Baselines are identical to the compiler before this change.Perf (issue repro,
--singleThreaded, check time / peak RSS)Partial<{ … }>is duplicated when instantiated again: quadratic inference, spurious TS2859, repeated union members #65560