Repository navigation
fix(types): cut type-instantiation cost for polymorphic styled components - #5768
Conversation
…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.
🦋 Changeset detectedLatest commit: dea02ad 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 |
This comment was marked as low quality.
This comment was marked as low quality.
There was a problem hiding this comment.
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
ThemedExecutionPropsshape to avoid repeatedly computingExecutionPropsminusas/forwardedAsthrough mapped/omit types. - Reworks
PolymorphicCallPropsfrom a single three-way conditional into two intersected conditionals withunknownabsorption to avoid instantiating the heavyas-target branch for plain call sites. - Adds a changeset for a patch release documenting the user-observable impact (lower
tscmemory/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.
|
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. Mantineas 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 👍 |
* 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]>
Fixes #5767.
6.4.3 introduced a TypeScript type-checking regression: every
styled(...)call site began routing through a single three-way conditional whoseas-target branch is instantiated even for plain, non-polymorphic usage. In codebases heavy onstyled(Component)wrappers and polymorphicas, this inflatedtscinstantiation 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, plusasusage):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 theas/forwardedAstype parameters are still uninferred, the checker probes each conditional through its constraint union, andunion(branch, unknown)absorbs tounknown, so plain sites no longer instantiate the heavyas-target branch on every probe. After inference the result is identical, becauseX & unknownreduces toX. A flat{ as?; forwardedAs? }member preserves per-attribute contextual typing (literalas="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:typessuite, the ref-callback and polymorphicastype contracts, and JSX attribute autocompletion (including mid-typingas-target completion) all pass unchanged. The package's own type-check is clean.