Skip to content

fix(types): cut consumer type-check cost for polymorphic components - #5783

Merged
quantizor merged 2 commits into
6.xfrom
fix/type-perf-5767
Aug 4, 2026
Merged

quantizor merged 2 commits into
6.xfrom
fix/type-perf-5767

Conversation

@quantizor

@quantizor quantizor commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

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 of as call 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

types instantiations memory check time
6.4.4 baseline baseline baseline baseline
6.4.2 -81% -36% -73% -74%
this PR -28% -31% -63% -63%

100 components

types instantiations memory check time
6.4.4 baseline baseline baseline baseline
6.4.2 -78% -33% -66% -66%
this PR -28% -37% -64% -62%

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/react 19.

Nothing about the API changes. Autocomplete on as targets is unchanged, and so is every behavior pinned by the type contract suite, which passes unedited. Worth noting that mid-typing an attribute after as="video" still completes correctly here, which is behavior 6.4.2 did not have.

Verification

  • Full type contract suite, unedited. No @ts-expect-error was added or removed.
  • Full web and native test suites.
  • The as / forwardedAs / ref / attrs contracts re-checked against @types/react 19 as well as the pinned 18.
  • Editor behavior checked by driving a real tsserver at half-typed JSX attributes, with a positive control, since a tsc run 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.

Copilot AI review requested due to automatic review settings August 4, 2026 02:22
@changeset-bot

changeset-bot Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cb4a8bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 ComponentPropsWithRef for 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.

Comment thread packages/styled-components/src/types.ts Resolved
Comment thread packages/styled-components/src/types.ts Resolved
Comment thread .changeset/wild-hoops-shake.md Resolved
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
@quantizor
quantizor force-pushed the fix/type-perf-5767 branch from 473208f to 1dc3c2b Compare August 4, 2026 02:48
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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

  • ForwardedAsTargetProps is derived via TargetProps<ForwardedAsTarget>, which includes ref (e.g. React.JSX.IntrinsicElements['div'] includes ref). 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.ts now relies on TypeScript’s built-in NoInfer (TS 5.4+). That means consumers on TS <5.4 will see a type error even if they don’t use NoInfer directly, 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.

@quantizor

Copy link
Copy Markdown
Contributor Author

@copilot The NoInfer issue was settled with an alias on the deep export

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@copilot The NoInfer issue was settled with an alias on the deep export

Confirmed — the alias is in place at packages/styled-components/src/utils/noInfer.ts and re-exported from types.ts via export type { NoInfer } from './utils/noInfer'. That way the deep path styled-components/dist/types continues to resolve, import { NoInfer } from 'styled-components' picks it up via index, and the two in-file uses of NoInfer inside types.ts still bind to the TypeScript intrinsic (a re-export creates no local binding). CI is green.

@quantizor
quantizor merged commit 2949923 into 6.x Aug 4, 2026
9 checks passed
@quantizor
quantizor deleted the fix/type-perf-5767 branch August 4, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants