Skip to content

fix(types): cut type-instantiation cost for polymorphic styled components - #5768

Merged
quantizor merged 2 commits into
6.xfrom
fix/polymorphic-callprops-type-perf
Jul 18, 2026
Merged

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

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

Fixes #5767.

6.4.3 introduced a TypeScript type-checking regression: every styled(...) call site began routing through a single three-way conditional whose as-target branch is instantiated even for plain, non-polymorphic usage. In codebases heavy on styled(Component) wrappers and polymorphic as, this inflated tsc instantiation counts and memory, up to out-of-memory in the worst cases.

This restructures the polymorphic call-props resolution so plain call sites stop paying for the as-target branch, with no change to the emitted types, the public API, or runtime behavior.

Impact (synthetic fixtures, TS 5.9.3)

On a workload representative of the report (reused styled(Component) over wide discriminated-union props, plus as usage):

Instantiations Memory
6.4.3 4.36M 1022 MB
this PR 3.05M (-30%) 940 MB
6.4.2 (pre-regression) 2.96M 782 MB

That recovers ~94% of the regression on that shape. A plain-styled.tag-heavy workload shows a smaller -4.4%, since those sites barely regressed to begin with.

How

The three-way conditional is split into two intersected conditionals whose untaken side is unknown. While the as/forwardedAs type parameters are still uninferred, the checker probes each conditional through its constraint union, and union(branch, unknown) absorbs to unknown, so plain sites no longer instantiate the heavy as-target branch on every probe. After inference the result is identical, because X & unknown reduces to X. A flat { as?; forwardedAs? } member preserves per-attribute contextual typing (literal as="video" inference), and both discriminants are kept positive so spreading a styled component's own props stays branch-safe for ref-callback inference. The structure is load-bearing and documented inline so it is not "simplified" back into the slower form.

Verification

No test changes were needed: behavior is identical. The full test:types suite, the ref-callback and polymorphic as type contracts, and JSX attribute autocompletion (including mid-typing as-target completion) all pass unchanged. The package's own type-check is clean.

…ents

Plain call sites no longer instantiate the `as`-target branch, recovering most of the 6.4.3 type-check regression for `styled(Component)` and `as` usage while keeping every type behavior identical. Fixes #5767.
Copilot AI review requested due to automatic review settings July 18, 2026 21:10
@changeset-bot

changeset-bot Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dea02ad

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

@cursor

This comment was marked as low quality.

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.

Pull request overview

Reduces TypeScript type-checking overhead for polymorphic styled components by restructuring how call-site props are resolved, addressing the 6.4.3 regression reported in #5767 while keeping the public surface and emitted types intended to remain equivalent.

Changes:

  • Introduces a pre-resolved ThemedExecutionProps shape to avoid repeatedly computing ExecutionProps minus as/forwardedAs through mapped/omit types.
  • Reworks PolymorphicCallProps from a single three-way conditional into two intersected conditionals with unknown absorption to avoid instantiating the heavy as-target branch for plain call sites.
  • Adds a changeset for a patch release documenting the user-observable impact (lower tsc memory/time in large codebases) without runtime changes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/styled-components/src/types.ts Refactors polymorphic call-props typing to reduce instantiation cost while preserving the intended overload-equivalent shapes.
.changeset/polymorphic-callprops-type-perf.md Adds a patch changeset describing reduced TypeScript checking cost for heavy styled() usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@quantizor
quantizor merged commit 537ea42 into 6.x Jul 18, 2026
7 checks passed
@quantizor
quantizor deleted the fix/polymorphic-callprops-type-perf branch July 18, 2026 23:07
@eMerzh

eMerzh commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix .. here the cost of the "as" prop is still pretty high, as we don't really use it we've patch style-component ... something looking like

diff --git a/dist/types.d.ts b/dist/types.d.ts
index 98a2c4d0f5bb111ba6523c9193028f7a69512179..46f7524b2c3346ad659227ac1480f375210cb3a9 100644
--- a/dist/types.d.ts
+++ b/dist/types.d.ts
@@ -174,10 +174,7 @@ export interface PolymorphicComponent<out R extends Runtime, in out BaseProps ex
     as?: StyledTarget<R> | undefined;
     forwardedAs?: StyledTarget<R> | undefined;
 }> {
-    <AsTarget extends StyledTarget<R> | (BaseProps extends {
-        as?: infer A;
-    } ? A : never) | void = void, ForwardedAsTarget extends StyledTarget<R> | void = void>(props: PolymorphicCallProps<R, BaseProps, AsTarget, ForwardedAsTarget>): React.JSX.Element;
-}
+} // patch: generic `as`/`forwardedAs` call signature removed, it made tsc ~12x slower
 /**
  * Some wrapped targets can't be statically introspected and their props
  * collapse to `{}` -- most notably polymorphic-factory components (e.g. Mantine

as the comment says, tsc then goes from 60s ish to 5s ish for our code base...

i was wondering is a solution with a global type flag to enable/disable it wouldn't be interesting.

@quantizor

Copy link
Copy Markdown
Contributor Author

Thanks for the fix .. here the cost of the "as" prop is still pretty high, as we don't really use it we've patch style-component ... something looking like

diff --git a/dist/types.d.ts b/dist/types.d.ts
index 98a2c4d0f5bb111ba6523c9193028f7a69512179..46f7524b2c3346ad659227ac1480f375210cb3a9 100644
--- a/dist/types.d.ts
+++ b/dist/types.d.ts
@@ -174,10 +174,7 @@ export interface PolymorphicComponent<out R extends Runtime, in out BaseProps ex
     as?: StyledTarget<R> | undefined;
     forwardedAs?: StyledTarget<R> | undefined;
 }> {
-    <AsTarget extends StyledTarget<R> | (BaseProps extends {
-        as?: infer A;
-    } ? A : never) | void = void, ForwardedAsTarget extends StyledTarget<R> | void = void>(props: PolymorphicCallProps<R, BaseProps, AsTarget, ForwardedAsTarget>): React.JSX.Element;
-}
+} // patch: generic `as`/`forwardedAs` call signature removed, it made tsc ~12x slower
 /**
  * Some wrapped targets can't be statically introspected and their props
  * collapse to `{}` -- most notably polymorphic-factory components (e.g. Mantine

as the comment says, tsc then goes from 60s ish to 5s ish for our code base...

i was wondering is a solution with a global type flag to enable/disable it wouldn't be interesting.

I'll look into it 👍

stipsan added a commit to sanity-io/ui that referenced this pull request Jul 21, 2026
* fix(deps): update dependency styled-components to ^6.4.4

Co-authored-by: Cody Olsen <[email protected]>

* fix: make $flex optional in ResponsiveFlexItemStyleProps

styled-components 6.4.4 restructured its polymorphic call-site types
(styled-components/styled-components#5768): plain call sites now relate
against a single conditional signature that no longer lets required
style props of a styled(Box) wrapper go unchecked. Flex applies
flexItemStyle without passing $flex (its public flex prop is forwarded
to the inner Box, which resolves it to $flex), so the prop must be
optional. flexItemStyle already guards for the missing value at
runtime.

Co-authored-by: Cody Olsen <[email protected]>

* chore(deps): changeset for dependency update

Co-authored-by: Cody Olsen <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Cody Olsen <[email protected]>
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