Skip to content

feat(types): add StyledComponent for explicit export annotations - #5799

Merged
quantizor merged 3 commits into
6.xfrom
feat/public-styled-component-type
Aug 15, 2026
Merged

quantizor merged 3 commits into
6.xfrom
feat/public-styled-component-type

Conversation

@quantizor

@quantizor quantizor commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

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:

import styled, { type StyledComponent } from 'styled-components';

export const Card: StyledComponent<'div', { $active?: boolean }> = styled.div<{ $active?: boolean }>`...`;
export const CloseButton: StyledComponent<typeof IconButton> = styled(IconButton)`...`;

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(…). Props is 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 in Props; an object-form as redirect adds the resolved target's props to Props.

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.

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-bot

changeset-bot Bot commented Aug 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c2b5a64

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 Minor

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.

@pullfrog pullfrog Bot 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.

ℹ️ Minor suggestions only.

Reviewed changes

  • StyledComponent / NativeStyledComponent aliases — StyledComponent<Target, Props> (web) and NativeStyledComponent (aliased as StyledComponent on the native entry), each resolving to exactly what styled(Target)<Props> produces for the no-attrs arm, with thorough JSDoc covering the .attrs() spelling rules.
  • Contract tests — _ScExact/_scExact in test/types.tsx pin mutual assignability (identity, given the invariant Props) across every target kind and construction arm; native round-trip tests added alongside.
  • Type-perf fixtures & budgets — three annotatedPublic* kinds added to KIND_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?

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes (delta since the prior pullfrog review)

  • 6b343e59 docs(type-perf) — adds a docs/type-performance.md spec section describing the three annotatedPublic* fixture kinds, rewrites the KIND_WEIGHTS comment in typePerf.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.
  • c2b5a647 docs(types) — trims the StyledComponent/NativeStyledComponent JSDoc in types.ts, the contract-block comments in test/types.tsx and test/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).

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor merged commit 7ce6aea into 6.x Aug 15, 2026
7 checks passed
@quantizor
quantizor deleted the feat/public-styled-component-type branch August 15, 2026 22:29
quantizor added a commit that referenced this pull request Aug 17, 2026
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.
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.

1 participant