Repository navigation
fix(types): cut consumer type-check cost for polymorphic components - #5783
Conversation
🦋 Changeset detectedLatest commit: cb4a8bf The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
🟡 Not ready to approve
It appears to remove a previously-exported public type (NoInfer) while claiming no API change (and shipping as a patch), which risks unintended breaking impact for consumers.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
Reduces TypeScript type-check cost for consumers using polymorphic styled components (especially styled(Component) wrapping and as), and adds a CI budget check to prevent future type-performance regressions.
Changes:
- Refactors polymorphic target prop extraction to use a single distributive conditional and avoid expensive
ComponentPropsWithReffor intrinsic elements. - Adds a consumer-shaped “type-perf” fixture + budget file, wires it into
pnpm --filter styled-components type-perf, and runs it in CI after building declarations. - Documents the type-performance guardrails and ignores the generated fixture directory.
File summaries
| File | Description |
|---|---|
| packages/styled-components/type-perf.budget.json | Adds an initial budget for consumer type/instantiation counts. |
| packages/styled-components/src/types.ts | Introduces TargetProps to lower polymorphic type cost; adjusts polymorphic prop extraction defaults. |
| packages/styled-components/scripts/typePerf.mjs | Adds a script that generates a consumer-like fixture and enforces a committed TS perf budget. |
| packages/styled-components/package.json | Exposes the type-perf script for local/CI usage. |
| AGENTS.md | Documents the new perf budget command and updated type-performance guidance. |
| .gitignore | Ignores the generated .type-perf fixture directory. |
| .github/workflows/test.yml | Runs the new consumer type-perf budget check in CI after pnpm build. |
| .changeset/wild-hoops-shake.md | Adds a patch changeset describing the improved type-check performance. |
Review details
- Files reviewed: 7/8 changed files
- Comments generated: 4
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
Target props are now derived by one flat conditional that resolves HTML tags through an indexed access on JSX.IntrinsicElements, instead of two nested conditionals bottoming out in ComponentPropsWithRef. The old shape was distributive with a ~153-member target union in its constraint, so every constraint probe re-instantiated the conditional once per union member. Each of those instantiations dragged in ComponentPropsWithRef, which on @types/react 18 rebuilds the entire prop bag through Omit purely to strip legacy string refs. On a 100-component fixture that made ComponentPropsWithRef roughly a third of the program's whole type population, up more than 100x from 6.4.2. Net on a 500-component fixture, measured against the built declarations: check time drops to about a third of 6.4.4's and memory to about a third, putting instantiation count slightly under 6.4.2's. Bracketing the conditional is not the fix and makes things worse: the `T & KnownTarget` needed to restore narrowing cross-products two ~153-member unions and exhausts the heap. Folding the KnownTarget test into the helper's own AnyComponent arm is what makes it cheap; nesting a check back on top costs roughly 4x the check time. The deprecated NoInfer alias moves to its own module and is re-exported. Declaring it alongside its own uses shadowed the TypeScript intrinsic within that file and downgraded both hot references to the slower deferred form; a re-export introduces no local binding. It was never exported from the package entry, but resolves via a deep import that at least one published library relies on, so it is kept. Adds a consumer type-check budget to CI, measured against the built declarations, because nothing in the existing suites can observe this class of regression. Fixes #5767
473208f to
1dc3c2b
Compare
The guard was `keyof K extends never`, but `K` is the set of attrs keys and is `never` for any component without `.attrs()`. `keyof never` is `string | number | symbol`, so the short-circuit never fired: every such component instantiated an omit plus a `Partial<Pick<...>>` that removed and re-added nothing, and intersected the empty object into its own props, which never reduces. Present since well before this branch. Removing it drops instantiations by about 15% and check time by about 10% on top of the rest, and roughly halves the length of the type shown on hover.
There was a problem hiding this comment.
🟡 Not ready to approve
There’s a user-visible TypeScript-compatibility implication (TS 5.4+ NoInfer) that should be explicitly called out, and there’s at least one misleading updated comment in the public types.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Review details
Suppressed comments (2)
packages/styled-components/src/types.ts:222
ForwardedAsTargetPropsis derived viaTargetProps<ForwardedAsTarget>, which includesref(e.g.React.JSX.IntrinsicElements['div']includesref). The inline comment saying ref is excluded is misleading and makes it harder to reason about polymorphic ref behavior.
// props extracted from "forwardedAs"; note that ref is excluded
.changeset/wild-hoops-shake.md:5
- The public
.d.tsnow relies on TypeScript’s built-inNoInfer(TS 5.4+). That means consumers on TS <5.4 will see a type error even if they don’t useNoInferdirectly, which is a user-visible compatibility change that isn’t mentioned here.
Large TypeScript projects type-check dramatically faster. On a 500-component app, `tsc` check time drops to about a third of what 6.4.4 takes and peak memory to about a third, which resolves the out-of-memory failures some projects hit after upgrading past 6.4.2. Editor responsiveness improves by the same margin, and autocomplete on `as` targets is unchanged.
- Files reviewed: 8/9 changed files
- Comments generated: 0 new
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
|
@copilot The NoInfer issue was settled with an alias on the deep export |
Confirmed — the alias is in place at |
Fixes #5767.
Projects that upgraded past 6.4.2 saw TypeScript get slower and hungrier, badly enough that some builds started running out of memory and people pinned back to 6.4.2. This brings the cost back down and then some.
Measured on a fixture in the shape reporters described (heavy
styled(Component)wrapping, attrs chains, and a minority ofascall sites), type-checked against the built declarations the way a consumer resolves them. Median of repeated cold runs, TypeScript 5.9.3. Figures are given as ratios against 6.4.4 so they do not rot.500 components
100 components
Check time and memory both land at roughly a third of 6.4.4, which should clear the out-of-memory failures reported at
--max-old-space-size=3072. Instantiation count is now slightly below 6.4.2's. The same holds under@types/react19.Nothing about the API changes. Autocomplete on
astargets is unchanged, and so is every behavior pinned by the type contract suite, which passes unedited. Worth noting that mid-typing an attribute afteras="video"still completes correctly here, which is behavior 6.4.2 did not have.Verification
@ts-expect-errorwas added or removed.as/forwardedAs/ ref / attrs contracts re-checked against@types/react19 as well as the pinned 18.tscrun cannot see completions.Preventing a repeat
This class of regression was invisible to every existing suite, which is why it shipped. This adds
pnpm --filter styled-components type-perf, which generates the fixture, checks it against the built declarations, and fails when types or instantiations exceed a committed budget. It runs in CI after the build. Verified both ways: it passes on this branch and rejects the 6.4.4 numbers.Thanks to @binhpv, @develra, and @litvinenko-rc for the bisections and benchmarks on the issue. The attribution to a single symbol is what made this findable.