Repository navigation
feat(types): add StyledComponent for explicit export annotations - #5799
Conversation
Packages that emit their own declarations, including any project using isolatedDeclarations, must annotate every exported styled component, and there was no public type for it: consumers reached into styled-components/dist/types or hand-assembled a shape that silently dropped members such as hoisted statics. StyledComponent<Target, Props> is now exported from the web and native entries. It resolves to the exact type styled(Target)<Props> produces across every target kind, .withConfig, and hoisted statics, so the annotation is not lossy and the check against the inferred component stays cheap. Contract tests enumerate the target-kind matrix and the two .attrs() spelling rules; three annotated-public type-perf fixture kinds guard the alias's construction arms (intrinsic tag, statics wrap, factory widening) with both budgets re-measured.
🦋 Changeset detectedLatest commit: c2b5a64 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 |
The type-performance guide enumerates the fixture kinds; add the three annotatedPublic* kinds introduced alongside StyledComponent so the enumeration matches the fixture.
There was a problem hiding this comment.
ℹ️ Minor suggestions only.
Reviewed changes
StyledComponent/NativeStyledComponentaliases —StyledComponent<Target, Props>(web) andNativeStyledComponent(aliased asStyledComponenton the native entry), each resolving to exactly whatstyled(Target)<Props>produces for the no-attrs arm, with thorough JSDoc covering the.attrs()spelling rules.- Contract tests —
_ScExact/_scExactintest/types.tsxpin mutual assignability (identity, given the invariantProps) across every target kind and construction arm; native round-trip tests added alongside. - Type-perf fixtures & budgets — three
annotatedPublic*kinds added toKIND_WEIGHTS(count unchanged at 100) and both type-perf budgets re-measured, moving down.
I traced the new aliases against the constructor's return arm in constructWithOptions.ts and confirmed they are structurally identical: same WidenForUntypedTarget<TargetProps<R, Target>, MergeProps<...>> computation with TargetProps correctly passed as the widen input (the #5756 footgun is not repeated), same & (Target extends string ? {} : Omit<Target, keyof React.Component<any>>) statics intersection on web, and BaseObject statics defaults. The .attrs() divergence is real but honestly documented, and each documented spelling is pinned by a contract test. The measured budget drop is consistent with identity-cheap annotatedPublic* fixtures replacing what those residues previously measured, and CI recomputes deterministically, so it is not gamed.
ℹ️ annotatedPublic* perf fixtures canary only one assignment direction
Each of the three new annotatedPublic* fixtures writes const C: StyledComponent<...> = styled<...>(...) and relies on the compile-clean gate. That checks the direction constructor type → alias: a StyledComponent that drifted to be narrower than the constructor fails the assignment and trips the canary. The inverse direction (the alias drifting to be a wider superset of the constructor, e.g. dropping or relaxing a required prop) still compiles clean and would be invisible to these fixtures — it is only caught by the explicit bidirectional _ScExact blocks in test/types.tsx, which run under the separate test:types job. The round-trip coverage exists, so I don't think a change is required here; flagging so the canary's exact scope is understood.
Technical details
# One-directional canary in the annotatedPublic* fixtures
## Affected sites
- packages/styled-components/scripts/typePerf.mjs:290-313 (new `annotatedPublic`, `annotatedPublicWrap`, `annotatedPublicFactory` cases)
## Required outcome
- Confirm the inverse (too-wide alias) is intentionally left to `test:types.tsx`'s `_scExact` blocks rather than these fixtures; no change strictly required, but a too-wide drift should be known to be invisible to `type-perf`.
## Open questions for the human
- Is the cost of a back-direction reference per fixture worth closing this seam in the perf gate, or is the bidirectional `test:types` coverage sufficient?DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes (delta since the prior pullfrog review)
6b343e59docs(type-perf) — adds adocs/type-performance.mdspec section describing the threeannotatedPublic*fixture kinds, rewrites theKIND_WEIGHTScomment intypePerf.mjs(dropping the contradictory "identity-cheap vs. expensive" phrasing my prior inline note flagged), and trims the per-case fixture comments. Fixture behavior, kinds, weights, and budgets are unchanged.c2b5a647docs(types) — trims theStyledComponent/NativeStyledComponentJSDoc intypes.ts, the contract-block comments intest/types.tsxandtest/types.native.tsx, and the changeset prose. All still accurate; no type shapes or tests change.
Both commits are documentation-only with no behavioral surface. My prior inline thread on the typePerf.mjs comment was addressed by the reword and is resolved; the standalone informational note about the annotatedPublic* fixtures only guarding one assignment direction still stands but needs no change (the bidirectional _scExact blocks in test:types cover the inverse).
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
Lower both pending changesets (the StyledComponent annotation type from #5799 and the declaration-site type-check speedup) from minor to patch so the next stable release is a patch.

What
A public
StyledComponent<Target, Props>type for annotating explicitly-typed styled component exports, from the web and native entries.Why
Packages that emit their own declaration files, including any project using
isolatedDeclarations, must annotate every exported styled component. There was no public type for it, so consumers reached into internal paths (styled-components/dist/types) or hand-assembled one that dropped members like a wrapped component's hoisted statics.Name the target and props as you passed them to
styled:Guarantees
Resolves to the exact type
styled(Target)<Props>produces, verified by contract tests across every target kind (intrinsic tag, custom element, function, class,forwardRef,memo, another styled component, union-props and generic-polymorphic targets, an un-introspectable factory, disjoint unions) and.withConfig(…).Propsis invariant, so the tests assert identity, not one-way compatibility..attrs(…)carries data no annotation can see (backfilled keys, target redirect), covered by two documented rules, each pinned by a test: a backfilled required prop becomes optional inProps; an object-formasredirect adds the resolved target's props toProps.Only one new public name per entry; the internal helpers resolve inside the emitted declarations without entering the root namespace.
Type performance
Three
annotatedPublic*fixture kinds guard the alias's construction arms (intrinsic tag, statics wrap, factory widening); both budgets re-measured and moved down.