Skip to content

Commit 537ea42

Browse files
authored
fix(types): cut type-instantiation cost for polymorphic styled components (#5768)
* fix(types): cut type-instantiation cost for polymorphic styled components 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. * docs(types): trim polymorphic call-props comment to the invariant
1 parent e066341 commit 537ea42

2 files changed

Lines changed: 40 additions & 37 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"styled-components": patch
3+
---
4+
5+
Reduce TypeScript type-checking cost for styled components, most noticeably `styled(Component)` wrappers and polymorphic `as` usage. Large codebases that saw elevated `tsc` memory and type-instantiation counts get lower type-check memory and time, with no change to the emitted types or runtime behavior.

‎packages/styled-components/src/types.ts‎

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ export interface IStyledStatics<
179179
| undefined;
180180
}
181181

182+
/** ExecutionProps sans as/forwardedAs, pre-resolved so call sites relate against a concrete interface. */
183+
interface ThemedExecutionProps {
184+
theme?: DefaultTheme | undefined;
185+
}
186+
182187
/**
183188
* Used by PolymorphicComponent to define prop override cascading order.
184189
*/
@@ -206,52 +211,48 @@ export type PolymorphicComponentProps<
206211
keyof ExecutionProps
207212
>
208213
> &
209-
FastOmit<ExecutionProps, 'as' | 'forwardedAs'> & {
214+
ThemedExecutionProps & {
210215
as?: AsTarget;
211216
forwardedAs?: ForwardedAsTarget;
212217
}
213218
>;
214219

215220
/**
216-
* Resolves the call-site props for one usage of a polymorphic component,
217-
* branching on the supplied `as` / `forwardedAs` targets into the three prior
218-
* overload shapes:
219-
* - `as` is a real render target (e.g. `as="video"`, `as={Component}`):
220-
* substitute that target's props and require `as`.
221-
* - no `as`, or `as` is the wrapped component's own non-target type (e.g.
222-
* Next.js Link's `as?: Url`): Substitute-free base props so ref callbacks
223-
* infer with spread props (#5687), the wrapped `as` type stays assignable and
224-
* optional (#5734), and BaseProps keys keep completing for plain usage (#5741).
225-
* - `forwardedAs` only: substitute the forwarded target's props.
221+
* Resolves the call-site props for one usage of a polymorphic component from its
222+
* `as` / `forwardedAs` targets. An `as` render target substitutes that target's
223+
* props and requires `as`; plain usage (or `as` being the wrapped component's own
224+
* non-target type, e.g. Next.js Link's `as?: Url`) keeps Substitute-free base
225+
* props so ref callbacks infer with spread props (#5687), the wrapped `as` stays
226+
* assignable (#5734), and BaseProps keys keep completing (#5741); `forwardedAs`
227+
* substitutes the forwarded target's props.
226228
*
227-
* Extracted to a named alias so identical (R, BaseProps, AsTarget,
228-
* ForwardedAsTarget) tuples dedupe across the many JSX call sites in an app.
229+
* The target test is `string | AnyComponent`, not `KnownTarget`: narrowing it
230+
* drops custom element strings (`as="my-element"`) out of the target branch.
229231
*
230-
* The target test is `string | AnyComponent`, not the broader `WebTarget`
231-
* (`KnownTarget | (string & {})`): both describe the same set (any string or
232-
* component), but comparing against the ~156 literal members of `KnownTarget`
233-
* here costs ~6% more type instantiations across all call sites. Don't narrow it
234-
* to bare `KnownTarget` -- that drops custom element strings (`as="my-element"`)
235-
* out of the target branch and makes them error.
232+
* Load-bearing shape, do not simplify: two conditionals with `unknown` sibling
233+
* branches (not one three-way conditional), a leading flat `{ as?; forwardedAs? }`
234+
* member, and positive `extends [string | AnyComponent]` discriminants. Collapsing
235+
* the conditionals, dropping the flat member, or using a `[void]` discriminant
236+
* each regress plain-call-site cost, `as`-target completion, or ref-callback
237+
* inference (#5687) respectively.
236238
*/
237239
type PolymorphicCallProps<
238240
R extends Runtime,
239241
BaseProps extends BaseObject,
240242
AsTarget extends StyledTarget<R> | (BaseProps extends { as?: infer A } ? A : never) | void,
241243
ForwardedAsTarget extends StyledTarget<R> | void,
242-
> = [AsTarget] extends [string | AnyComponent]
244+
> = { as?: AsTarget | undefined; forwardedAs?: ForwardedAsTarget | undefined } & ([
245+
AsTarget,
246+
] extends [string | AnyComponent]
243247
? PolymorphicComponentProps<R, BaseProps, AsTarget, ForwardedAsTarget> & { as: AsTarget }
244-
: [ForwardedAsTarget] extends [void]
245-
? OverrideStyle<
246-
NoInfer<FastOmit<BaseProps, keyof ExecutionProps>> &
247-
FastOmit<ExecutionProps, 'as' | 'forwardedAs'> & {
248-
as?: BaseProps extends { as?: infer A } ? A : void;
249-
forwardedAs?: BaseProps extends { forwardedAs?: infer A } ? A : void;
250-
}
251-
>
252-
: PolymorphicComponentProps<R, BaseProps, void, ForwardedAsTarget> & {
253-
forwardedAs: ForwardedAsTarget;
254-
};
248+
: unknown) &
249+
([AsTarget] extends [string | AnyComponent]
250+
? unknown
251+
: [ForwardedAsTarget] extends [string | AnyComponent]
252+
? PolymorphicComponentProps<R, BaseProps, void, ForwardedAsTarget> & {
253+
forwardedAs: ForwardedAsTarget;
254+
}
255+
: OverrideStyle<NoInfer<FastOmit<BaseProps, keyof ExecutionProps>> & ThemedExecutionProps>);
255256

256257
/**
257258
* This type forms the signature for a forwardRef-enabled component
@@ -274,12 +275,9 @@ export interface PolymorphicComponent<
274275
forwardedAs?: StyledTarget<R> | undefined;
275276
}
276277
> {
277-
// A single call signature (not several overloads) so JSX attribute completion
278-
// never collapses while an attribute is mid-typed. With multiple overloads, a
279-
// partially-typed or unknown attribute matches none of them, overload
280-
// resolution fails, and completion drops to nothing; one signature always
281-
// resolves, so the rendered target's props keep autocompleting. The branching
282-
// that preserves the prior overload shapes lives in `PolymorphicCallProps`.
278+
// A single call signature, not overloads: with overloads a mid-typed JSX
279+
// attribute matches none, resolution fails, and attribute completion drops. The
280+
// prop-shape branching lives in `PolymorphicCallProps`.
283281
<
284282
AsTarget extends StyledTarget<R> | (BaseProps extends { as?: infer A } ? A : never) | void =
285283
void,

0 commit comments

Comments
 (0)