Skip to content

6.4.3: PolymorphicCallProps rewrite causes 57× more ComponentPropsWithRef instantiations, +1.6 GB tsc heap in our app #5767

Done

Description

@develra

Hi, thanks for maintaining styled-components.

Filing this because in our monorepo the 6.4.2 → 6.4.3 bump added enough tsc pressure to start OOMing our dev-server type checker. Feel free to close if you knew about the trade-off and decided it was worthwhile so this is WAI.

We bisected on the same commit and same lockfile, swapping only styled-components between 6.4.2 and 6.4.3, running tsc --extendedDiagnostics --noEmit cold each time (deleted .tsbuildinfo before every run, alternated the order A→B→A→B to rule out caching artifacts):

Metric 6.4.2 (run 1) 6.4.3 (run 1) 6.4.2 (run 2) 6.4.3 (run 2)
Types 1,487,074 1,892,984 1,487,074 1,892,984
Instantiations 33,223,024 47,784,078 33,223,024 47,784,078
Symbols 3,701,255 3,703,629 3,701,255 3,703,629
Memory used 3,427 MB 5,244 MB 3,555 MB 5,133 MB
Check time 70.0s 91.4s 70.9s 103.2s

Structural metrics are bit-identical per-version; memory/time jitter run-to-run as expected. Net effect of the 6.4.2 → 6.4.3 bump:

  • +406,282 types (+27%)
  • +14,561,054 instantiations (+44%)
  • +1.6 GB heap (+45%)
  • +22–33s check time (+30–45%)

We used --generateTrace to attribute the growth. It is not spread across the codebase — it is concentrated in a single symbol:

Symbol (from @types/react) 6.4.2 6.4.3 Delta
ComponentPropsWithRef 4,428 252,248 +247,820 (57×)

By flag: +258,286 new Conditional types and +146,454 new Intersection types, essentially entirely rooted in @types/react instantiations from styled-components.

Comparing styled-components/dist/types.d.ts between the two versions, the change is the new PolymorphicCallProps alias and the widened AsTarget bound in PolymorphicComponentProps (referenced in your own comment as ~6% more instantiations across call sites):

// 6.4.3 additions
export type PolymorphicComponentProps<R, BaseProps,
  AsTarget extends StyledTarget<R> | (BaseProps extends { as?: infer A } ? A : never) | void,
  ForwardedAsTarget extends StyledTarget<R> | void,
  AsTargetProps extends BaseObject = AsTarget extends KnownTarget ? React.ComponentPropsWithRef<AsTarget> : {},
  ForwardedAsTargetProps extends BaseObject = ForwardedAsTarget extends KnownTarget ? React.ComponentPropsWithRef<ForwardedAsTarget> : {},
> = ...

type PolymorphicCallProps<R, BaseProps, AsTarget, ForwardedAsTarget> =
  [AsTarget] extends [string | AnyComponent] ? PolymorphicComponentProps<...> & { as: AsTarget }
  : [ForwardedAsTarget] extends [void] ? OverrideStyle<...>
  : PolymorphicComponentProps<R, BaseProps, void, ForwardedAsTarget> & { forwardedAs: ForwardedAsTarget };

Every JSX usage of a styled component now goes through the three-branch conditional, and each branch that lands on the target-props branch re-instantiates React.ComponentPropsWithRef (already a known TS hotspot). The comment on the type acknowledges the tradeoff for
KnownTarget narrowing but the multiplier we see in a real app is much larger than 6%.

Scale of usage on our side: a large SPA, heavy styled(Component) and styled.div usage across ~2000 components.

Repro: any large codebase with heavy styled() usage; alternate installs of 6.4.2 and 6.4.3 with .tsbuildinfo deleted each time, run tsc --extendedDiagnostics, and diff the numbers. ComponentPropsWithRef count via --generateTrace should show the jump.

Mitigation on our side: pinned to 6.4.2 for now.

Activity

  1. litvinenko-rc commented on Jul 8, 2026

    @litvinenko-rc

    I've noticed the same problem after updating from 6.4.2 to 6.4.3.
    Downgrade from 6.4.3 to 6.4.2 produced the following results:

      Total time:       40.43s -> 36.08s       delta -4.35s (-10.8%)
      Check time:       34.46s -> 30.04s       delta -4.42s (-12.8%)
      Memory used:    3566025K -> 3315636K     delta -250389K (-7.0%)
      Types:          1236436 -> 1105558       delta -130878 (-10.6%)
      Instantiations: 6750606 -> 5118007       delta -1632599 (-24.2%)
    
  2. added 2 commits that reference this issue on Jul 18, 2026
    209f2fd
    537ea42
  3. quantizor commented on Jul 18, 2026

    @quantizor
    Contributor

    Sorry about this, it should be greatly improved in 6.4.4.

  4. binhpv commented on Jul 20, 2026

    @binhpv

    Following up with verification numbers for 6.4.4, since it was closed on the expectation that 6.4.4 fixes this. On our codebase the instantiation regression is partially fixed, but tsc peak memory and wall-time are not — 6.4.4 is actually the heaviest and slowest of the whole 6.4.x line for us.

    Setup: a React component library (styled-components used heavily across the lib), TypeScript 6.0.3, @types/react 19.x. Same commit and lockfile, only the styled-components version swapped via the pnpm catalog. Each run cold (.tsbuildinfo deleted), tsc --noEmit --extendedDiagnostics, --max-old-space-size=8192, 2 runs per version. Structural metrics were bit-identical per version; peak RSS via /usr/bin/time -l (macOS).

    Version Types Instantiations Peak RSS Check time
    6.4.1 815,914 7,002,358 ~3.01 GB ~16.2s
    6.4.2 677,884 6,789,251 ~3.04 GB ~13.5s
    6.4.3 902,583 11,198,164 ~3.38 GB ~18.9s
    6.4.4 947,829 9,434,248 ~4.06 GB ~20.7s

    (tsc's own "Memory used" jitters with GC timing, so I'm reporting peak RSS as the stable memory signal; the "Memory used" figure showed the same trend, ~2.6 GB on 6.4.1 → ~3.96 GB on 6.4.4.)

    What this shows:

    • 6.4.2 is the leanest baseline; 6.4.3 is the regression this issue reported (instantiations 6.79M → 11.2M, +65%).
    • 6.4.4 partially fixes instantiations (11.2M → 9.43M, −16% vs 6.4.3) — the fix direction is real. 👍
    • But peak memory and time got worse, not better, vs 6.4.3: peak RSS 3.38 → 4.06 GB (+20%), check time up, and Types is now the highest of any 6.4.x (948K). So the "lower type-check memory and time" outcome doesn't hold here — 6.4.4 moved further from the pre-regression 6.4.2 baseline rather than back toward it. It looks like fewer instantiations were traded for larger/more-retained type objects.

    Concrete impact: our build pins tsc at --max-old-space-size=3072; 6.4.1/6.4.2 fit comfortably, 6.4.4 OOMs with FATAL ERROR: ... JavaScript heap out of memory.

    Happy to run --generateTrace and share the hot-symbol attribution if that would help localize where the remaining growth in 6.4.4 is concentrated.

  5. binhpv commented on Jul 20, 2026

    @binhpv

    @quantizor I was too excited for memory usage reduction and ran the benchmark in our repo. However the result is not as expected. Please see comment above.

  6. quantizor commented on Jul 20, 2026

    @quantizor
    Contributor

    I'll do what I can but honestly we are dealing with inefficiencies in TS itself that are structural. I think the new Go version of TS should be significantly better and definitely recommend trying it.

  7. develra commented on Jul 20, 2026

    @develra
    Author

    Memory impact is still pretty rough - even on TS 7.0.2 - (tsgo).

    Measured on TypeScript 7.0.2.

    Metric 6.4.2 (A1) 6.4.4 (B1) 6.4.2 (A2) 6.4.4 (B2) Δ (mean)
    Types 2,784,171 3,747,839 2,784,171 3,747,839 +35%
    Instantiations 22,289,990 23,687,865 22,289,990 23,687,865 +6.3%
    Symbols 6,792,702 6,787,777 6,792,702 6,787,777 -0.07%
    Memory used 5,422 MB 9,184 MB 5,431 MB 9,182 MB +69%
    Check time 11.7s 17.8s 13.4s 30.9s +94% (noisy)
    Total time 21.0s 28.5s 25.5s 47.8s +62% (noisy)

    I'll stick with 6.4.2 for now - but really appreciate the work towards a solution.

  8. quantizor commented on Jul 20, 2026

    @quantizor
    Contributor

    Thanks @binhpv and @develra, this is helpful. I'll keep chipping away at it.

  9. added 2 commits that reference this issue on Aug 4, 2026
    1dc3c2b
    2949923
  10. quantizor commented on Aug 6, 2026

    @quantizor
    Contributor

    There's a patch prerelease ([email protected]) that fixes a few other issues if you could test it and let me know if it fixes your issue?

  11. quantizor commented on Aug 7, 2026

    @quantizor
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions