Skip to content

perf(types): faster type-checking for annotated styled component exports - #5801

Merged
quantizor merged 3 commits into
6.xfrom
fix/declaration-site-type-perf
Aug 17, 2026
Merged

quantizor merged 3 commits into
6.xfrom
fix/declaration-site-type-perf

Conversation

@quantizor

@quantizor quantizor commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

When you annotate an exported styled component with an explicit type, type-checking is now meaningfully faster. This is the pattern that packages emitting their own declaration files rely on, including any project using isolatedDeclarations, where inference can't cross the declaration boundary and every export needs a written type.

The speedup scales with how many props the underlying target has, so it lands largest exactly where these codebases feel it today: components built on intrinsic tags like styled.div and styled.button, and wrappers over polymorphic components whose props are a union (a button/anchor, say). On the realistic consumer fixture the type instantiation count drops ~10%; on the heaviest annotation kinds it is closer to ~40% per component. Building components and rendering them is unchanged, this is purely the cost of the explicit annotation.

Nothing about the component's public type changes for your code. Props, defaultProps, propTypes, ref forwarding, and as polymorphism all behave exactly as before, and the public StyledComponent<Target, Props> annotation still resolves to the exact type styled(...) produces. It is a type-only, non-breaking change.

Verified green: the full type contract suite, the web and native runtime suites, and a set of declaration-site soundness checks (a wrong prop type is rejected, a wrong ref element is rejected, defaultProps stays narrowed, propTypes reads and writes still compile, as still resolves the target's props). Measured on TypeScript 5.9 and confirmed identical on the TypeScript 7 native compiler.

@changeset-bot

changeset-bot Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0251fa1

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

@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 — PolymorphicComponent switches its exotic base from React.ForwardRefExoticComponent to React.NamedExoticComponent so explicit declaration-site annotations (StyledComponent<Target, Props> / IStyledComponent<...>) stop walking ~265-key mapped members (defaultProps?: Partial<P>, propTypes) in both directions under the invariant Props. defaultProps is re-declared with the real narrow shape in IStyledComponentBase, so only the deprecated propTypes typing is given up. Consumer and augment type-perf budgets were re-measured (-10.2% / -9.5% instantiations); docs/type-performance.md documents the residual Partial<Props> walk and why attrs/defaultProps precision is deliberately retained; withTheme's ForwardRefExoticComponent base is correctly left unchanged.

I verified each load-bearing claim directly rather than relying on the benchmarks alone:

  • Only propTypes is actually dropped. React.ForwardRefExoticComponent<P> adds a single member over NamedExoticComponent<P> (propTypes under @types/react 19; defaultProps + propTypes under the 18 baseline). IStyledComponentBase re-declares defaultProps?: (ExecutionProps & Partial<Props>), and neither IStyledStatics nor CommonStatics declares propTypes, so nothing else is silently lost.
  • Ref forwarding is preserved. For a value-component type, ComponentPropsWithRef<T> falls through to the call-signature Props regardless of the exotic base (both bases are non-newable), and ComponentRef<StyledComponent<...>> already resolved to never before this change. The real ref typing rides in ComponentTargetProps via React.ComponentPropsWithRef<Target> over the target element, which the change never touches.
  • Exactness is preserved. The swap is symmetric (both the constructor output and the annotation derive from PolymorphicComponent), and the _scExact mutual-assignability suite (src/test/types.tsx ~L632+) pins annotation-vs-constructed identity across intrinsic tags, wrapped/class/memo/forwarded/polymorphic/attrs targets. The defaultProps precision claim is transitively pinned by those identity checks since it lives on the shared IStyledComponentBase.

The change is microbenchmark-backed on a realistic consumer fixture (satisfying the repo's bench-the-realistic-workload mandate), and the two changesets together disclose the only observable surface difference: the deprecated propTypes field's typing is gone.

Pullfrog  | 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 — The single new commit since the prior review (99a6d18b) closes the one API-parity gap the perf swap left behind: it re-declares defaultProps?: Partial<BaseProps> | undefined on the public PolymorphicComponent interface so the exported base type keeps the defaultProps member its ForwardRefExoticComponent base used to supply, while the deprecated propTypes typing stays removed. docs/type-performance.md and the inline types.ts comments were updated to match, and the committed budget figures (736,263 web / 810,440 augment) are unchanged.

  • Restored defaultProps on the public PolymorphicComponent — the re-declaration is additive over NamedExoticComponent (which exposes neither member) and is shadowed on real styled components by IStyledComponentBase's narrower defaultProps?: (ExecutionProps & Partial<Props>), so the per-annotation walk never re-enters the declaration-site relation, matching the claimed zero delta.
  • Docs and comments updated coherently — the changeset and docs/type-performance.md now state defaultProps typing behaves exactly as before, which holds both on the React 18 baseline (member restored) and React 19 (member added without conflict).

I cross-checked the load-bearing claims rather than trusting the benchmark: ForwardRefExoticComponent adds only propTypes over NamedExoticComponent under @types/react 19 (and defaultProps + propTypes under the 18 baseline), so the fix restores exactly the member the swap dropped; IStyledComponentBase (types.ts:433) extends PolymorphicComponent and redeclares defaultProps with the real ExecutionProps & Partial<Props> shape, confirming the shadowing argument; and the live budget files match the reported "unchanged" figures.

Pullfrog  | 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.

Caution

The latest commit's goal is to keep this perf swap non-breaking, but the propTypes member it re-adds references React.WeakValidationMap, a type that was removed from @types/react in v19. For React 19 consumers — fully within the supported peer range (react >= 16.8.0, no upper bound) — the published types now reference a namespace member that does not exist, producing TS2694 errors. I reproduced this directly: tsc --noEmit --project tsconfig.test-types.json against the installed @types/[email protected] fails at src/types.ts:388 with Namespace 'React' has no exported member 'WeakValidationMap'. This breaks the React 19 type surface and contradicts the changeset's claim that propTypes "behave exactly as before".

Reviewed changes — the delta since the prior review (4ac16f56, 2f8473eb):

  • Reframed the PR from breaking to non-breaking by keeping defaultProps?: Partial<BaseProps> | undefined and re-adding propTypes?: React.WeakValidationMap<any> | undefined on the public PolymorphicComponent, so no member is removed from the base type.
  • Rewrote docs/type-performance.md and the inline types.ts comments to attribute the cost to per-component parameterization (not member removal) and to document why the re-declared shapes relate cheaply.
  • Recast the faster-styled-component-annotations changeset to assert no public type surface changes, changed public-styled-component-annotation to patch, and re-measured the budget files (-9.9% web / -9.1% augment instantiations; ~+0.4% over dropping both members).
  • Added an AGENTS.md semver rule distinguishing additive (patch) from breaking (minor/major) type-only changes.

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

Comment thread packages/styled-components/src/types.ts Resolved
Comment thread packages/styled-components/src/types.ts Resolved
@quantizor
quantizor force-pushed the fix/declaration-site-type-perf branch from 2f8473e to 70b4cb5 Compare August 17, 2026 01:33

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

Caution

The blocking issue from the prior review is still unfixed: PolymorphicComponent re-declares propTypes?: React.WeakValidationMap<any>, but WeakValidationMap was removed from @types/react in v19. This delta is a rebase of the same content onto a newer 6.x base — no substantive change — so the break is still live.

Reviewed changes — since Pullfrog's last review at 2f8473eb, the branch was force-pushed/rebased onto base 7ce6aea4 and squashed into three commits (dffc416d perf type change, 21c21627 AGENTS.md semver rule, 70b4cb59 changeset minor→patch). Net content is unchanged from what was already reviewed:

  • PolymorphicComponent swaps its exotic base to React.NamedExoticComponent and re-declares defaultProps?: Partial<BaseProps> | undefined and propTypes?: React.WeakValidationMap<any> | undefined — identical to the previously reviewed state.
  • docs/type-performance.md added a section documenting the per-component parameterization cost and why the two members are re-declared in non-parameterized shapes.
  • AGENTS.md gains a semver rule for additive vs. breaking type-only changes; the public-styled-component-annotation changeset drops minor→patch.

Nothing in these commits addresses the previously flagged critical issue, so the PR still cannot merge as-is.

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

Comment thread packages/styled-components/src/types.ts Resolved

@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 — since Pullfrog's last review at 70b4cb59, a single commit (8251532b) landed that resolves the blocking React 19 break:

  • Fixed propTypes for React 19 consumers — PolymorphicComponent.propTypes changed from React.WeakValidationMap<any> | undefined to any. WeakValidationMap was removed from @types/react 19, and the peer range is react >= 16.8.0 with no upper bound, so any reference to it published a TS2694 into every React 19 consumer's build — defeating the commit's whole non-breaking goal. any is the one spelling that resolves across the supported range (it is also exactly what @types/react 19's own ForwardRefExoticComponent declares), stays assignable in both consumer usage directions, and remains a fixed type so the declaration-site relation still short-circuits. This is precisely the resolution the previous reviews recommended.
  • Docs and comments updated consistently — docs/type-performance.md and the inline types.ts comments now explain why propTypes must be any, note the cross-version hazard (the type suites run only against @types/react 18, so React-namespace changes need a manual 19 check), and keep the non-breaking reframing intact.
  • Budgets re-measured — type-perf.budget.json (738,335 web) and type-perf.augment.budget.json (812,680 augment) instantiations, both marginally lower than the prior figures, consistent with any being cheaper than WeakValidationMap<any>.

I re-confirmed the fix at src/types.ts:390 (propTypes?: any), verified the remaining WeakValidationMap references are only in explanatory comments (not declared members), and confirmed the doc/comment stories match the shipped type. The three previously-open review threads (the TS2694 break at line 388 and the stale portability comment) are all addressed by this commit and were resolved. No remaining WeakValidationMap type references exist in the public surface.

Pullfrog  | 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 critical issues — the new declaration gate is sound and load-bearing. One minor doc/precision nit inline.

Reviewed changes — the new commit since the prior approved review (8251532b) automates the "verify React-namespace changes against @types/react 19 by hand" hazard that the earlier any fix could only document:

  • Added a published-declaration gate — test:types:dist runs new scripts/typeCheckDist.mjs, compiling the emitted dist/index.d.ts + dist/native/index.d.ts under skipLibCheck: false against the pinned @types/react 18 and an aliased @types/react-19, and fails on any diagnostic in the package's own dist/ (dependency .d.ts noise filtered).
  • Wired it into the CI build job after pnpm build, added the @types/react-19 / @types/react-dom-19 aliased devDeps, and documented the command in AGENTS.md and docs/type-performance.md.

I validated the load-bearing claims directly rather than trusting the intent: the built package passes the gate on both 18 and 19, and a positive control (reintroducing propTypes?: React.WeakValidationMap<any> into dist/types.d.ts) reproduces the exact TS2694 on the 19 target with exit 1 while the 18 target stays clean — confirming the gate catches the bug that actually shipped and does not false-positive. Build (including the native DOM-isolation signature check) is green.

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

Comment on lines +48 to +49
// Minimum and maximum supported @types/react majors. The peer range is
// `react >= 16.8.0`; 18 is the pinned baseline the contract suite already covers,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "min and max supported @types/react majors" framing overstates the lower bound. The peer range is react >= 16.8.0, so @types/react@16 and @types/react@17 are in-support consumers, but the 18 target is the pinned baseline, not the floor. A type name the library newly uses that was only introduced in @types/react 18 would ship to React 16/17 consumers as their build's TS2694, and this gate (like the contract suite) would not catch it — the exact failure mode this PR exists to prevent, just in the opposite direction. The gate is exactly right for the removed-names case (19); consider either correcting the prose to "pinned baseline (18) + max (19)" (the same claim repeats in AGENTS.md:63 and the test.yml:51-54 comment) or, if an unbounded floor matters, adding 16/17 targets.

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

ℹ️ The refactor is correct and the gate stays load-bearing — one non-blocking operational consideration inline.

Reviewed changes — the single new commit since my prior review (63b2a6dc) reworks how the declaration gate resolves its max-major types:

  • Dropped the @types/react-19 / @types/react-dom-19 devDependency aliases (and their lockfile entries) and instead have the gate install the pinned max majors (@types/[email protected], @types/[email protected]) into a path-scoped, gitignored folder via npm install --no-save --no-package-lock, selecting them purely through paths + types: ['node'] and printing the resolved versionAt(...) in the run label. Min resolves from the project's own pinned @types/react 18.

This is the correct move: the aliased form I reviewed would have dropped a second copy of @types/react into node_modules/@types, and since this repo leaves types unset, TypeScript would auto-include both majors' ambient globals into every normal type-check. I re-verified the refactored gate in this environment — both targets compile clean (18.3.28 / 19.2.18), and the positive control (reintroducing propTypes?: React.WeakValidationMap<any> into dist/types.d.ts) still reproduces the exact TS2694 on the 19 target with exit 1 while 18 stays clean.

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

Comment on lines +60 to +67

/** Install the max-major @types into a path-scoped folder no other tsconfig sees. */
function isolatedMax() {
const root = join(workDir, 'max');
if (!existsSync(join(root, 'node_modules', '@types', 'react', 'package.json'))) {
mkdirSync(root, { recursive: true });
writeFileSync(join(root, 'package.json'), '{"private":true}\n');
execFileSync('npm', ['install', '--no-save', '--no-package-lock', MAX.react, MAX.reactDom], {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isolation is the right call, but it moves a live npm install into the build CI job at test time. GitHub Actions jobs are ephemeral, so workDir/max never survives between runs — the max-major @types (and its transitive, freshly-resolved deps like csstype, unpinned by any lockfile) is downloaded from the npm registry on every test:types:dist run, and a registry outage/rate-limit now fails the whole build job even when the code is fine. This is a reasonable tradeoff (instrumented infra like color: [react]-pinned targets and the repo's own scratch-npm i precedent), so non-blocking — but worth consciously accepting, e.g. whether a pnpm-store cache or an offline fallback is warranted before merge.

…orts

PolymorphicComponent extended React.ForwardRefExoticComponent, which
drags in the defaultProps/propTypes/$$typeof relation that TypeScript
re-checks at every annotated declaration site. Extending
React.NamedExoticComponent instead drops that inherited cost while
keeping the same call signature and ref forwarding.

defaultProps and propTypes are re-declared on the interface so nothing
about the emitted type changes for consumers. propTypes is typed as
`any` (matching React 19's own base component) rather than
WeakValidationMap, which @types/react removed in v19, so the declaration
compiles for both React 18 and 19 consumers.

Consumer budgets drop ~10%; intrinsic-tag and union-prop annotations
fall furthest.
… majors

The type-contract suite only compiles against the pinned @types/react
18, so a name that exists there but was removed on a newer major (as
WeakValidationMap was on 19) can ship into dist unnoticed and break
every strict React 19 consumer's build.

test:types:dist compiles the built .d.ts under skipLibCheck: false
against the minimum and maximum supported @types/react majors, failing
on any diagnostic located in the package's own dist/. The maximum major
installs into a gitignored, path-scoped folder so a second @types/react
never enters the repo's ambient type set; each run checks exactly one
major, chosen by explicit paths, and prints the resolved version. Wired
into CI after the type-perf checks.
…command

An additive type change ships as a patch (it cannot break a consumer's
build); a breaking type change is bumped as a breaking change even
though no runtime code moved. Also documents test:types:dist so the
cross-major declaration gate is discoverable.
@quantizor
quantizor force-pushed the fix/declaration-site-type-perf branch from 55dc137 to 0251fa1 Compare August 17, 2026 03:26
@pullfrog

pullfrog Bot commented Aug 17, 2026

Copy link
Copy Markdown

the model provider had a temporary upstream failure.

The provider refused or dropped the request mid-run — nothing in your repo or your credentials is at fault. Re-trigger Pullfrog; if it keeps happening, check the provider's status page.

provider error: {"code":502,"message":"Upstream error from Decart: Internal server error","metadata":{"error_type":"provider_unavailable"}}

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor merged commit 3de03c2 into 6.x Aug 17, 2026
8 checks passed
@quantizor
quantizor deleted the fix/declaration-site-type-perf branch August 17, 2026 17:52
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