only build intersection props that can reduce it to never - #64476
Draft
Max Schwenk (maschwenk) wants to merge 4 commits into
Draft
Max Schwenk (maschwenk) wants to merge 4 commits into
Max Schwenk (maschwenk) wants to merge 4 commits into
Conversation
getReducedType decided whether an intersection reduces to never by creating the combined property of every property name of every constituent (getPropertiesOfUnionOrIntersectionType). In large intersections most of those synthetic properties, and the intersections of their types, are never used again. Only a combination of distinct constituent properties where one has a literal type (a discriminant), a private property, or a constituent property that is itself such a combination can satisfy isNeverReducedProperty. isNeverReducedIntersection now collects the constituent properties of each name as createUnionOrIntersectionProperty does, including comparing instantiations of the same property, and only creates the combined property for those names. When the properties of the intersection are already resolved it checks them as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Sep 27, 2026
Instead of collecting the constituent properties of every name the way createUnionOrIntersectionProperty does, create the combined property for any name that more than one constituent has. The combined property of a name only one constituent has is that constituent's own property, which can only reduce the intersection if it already combines the properties of a union. This creates more combined properties than before, but is much simpler and saves the per-name lookups in every constituent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This reverts d33ab85. The simpler check was faster on the typescript-benchmarking projects, but on a 37k-file program it created 356k more symbols and held 0.28 GiB more heap (about 1.3 GiB more peak RSS with 4 checkers), which is the problem this change is for. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
isNeverReducedIntersection re-implemented how createUnionOrIntersectionProperty collects each constituent's property of a name. That loop is now collectUnionOrIntersectionPropertyParts, and both use it. From what it collects, isNeverReducedIntersection checks whether the combined property could satisfy isNeverReducedProperty (it needs a literal-typed or a private constituent property) before creating it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #64474 (part 2, #64475 is part 1). doesnt depend on #64475
getReducedTypechecks whether an intersection reduces toneverby callinggetPropertiesOfUnionOrIntersectionType, which creates the combined property (and the intersection of its types) for every name in the intersection and keeps all of them around. but a combined property can only reduce the intersection if one of the constituent properties has a literal type or is privateso this moves the loop in
createUnionOrIntersectionPropertythat collects each constituent's property of a name intocollectUnionOrIntersectionPropertyParts, and both use it.getReducedTypelooks at what it collected and only creates the combined property when it could reduce the intersection. theres no second copy of the checking logic. the code change is about 80 lines, the rest of the diff is one test and its baselinessimilar goal to #53346, but instead of stopping early it skips creating the properties that can't reduce, which is where the memory goes
this PR alone vs main (typescript-benchmarking, median of 3):
mui-docs gets the big win since its styling types build some really big intersections. the check times on the small projects are within run to run noise. raw results and scripts: https://gist.github.com/maschwenk/c83d9185c6961b6fb43d7d071ef39cf6
i also tried an even simpler check, create the property for any name more than one constituent has (d33ab85). less code, but on our 37k-file program it held 0.28 GiB more heap, so i went back
full go suite passes (also multiple checkers and
-race), lint and format are clean. addedintersectionNeverReductionByProperty.tswith baselines generated on unmodified main. it covers literal/boolean/template literal discriminants, optional andneverprops, private vs protected, instantiations of the same generic class, generic aliases and type params with union constraintsgo test ./...intsc, same thinghereby testruns)hereby linthereby check:formatused claude code to help write this, ive reviewed it