Skip to content

fix: support template index signatures - #5797

Merged
quantizor merged 2 commits into
styled-components:6.xfrom
cyphercodes:fix/5796-template-index-signature
Aug 15, 2026
Merged

quantizor merged 2 commits into
styled-components:6.xfrom
cyphercodes:fix/5796-template-index-signature

Conversation

@cyphercodes

Copy link
Copy Markdown
Contributor

Summary

Fix TypeScript's TS2590 error when applications extend React HTML props with a data-* template-literal index signature. Styled intrinsic elements continue to accept the augmented data attributes and CSS custom properties.

Fixes #5796

Testing

  • pnpm --filter styled-components test:types
  • pnpm build
  • pnpm --filter styled-components type-perf
  • pnpm test
  • pnpm --filter styled-components prettier
  • git diff --check

An AI coding assistant helped investigate the type regression and prepare the patch. I reviewed the resulting diff and verification output.

@changeset-bot

changeset-bot Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b9e0fb4

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.

ℹ️ The fix resolves the reported HTML case and compiles cleanly. One coverage note inline.

Reviewed changes

  • src/types.ts — IntrinsicProps now branches on a new IntrinsicElementsHaveDataIndex flag; when HTMLAttributes carries a data-* template-literal index signature it uses a new WithCSSVarsForDataIndex widening (intersection, no Omit over the template key) instead of WithCSSVars, sidestepping the union-complexity limit behind TS2590 (#5796).
  • test-types/template-index-signature.tsx + tsconfig.test-template-index.json — new isolated compile-only type suite augmenting HTMLAttributes with [dataAttribute: data-${string}] and exercising styled.div with a data-* attribute and a CSS custom property.
  • package.json — test:types now also runs the new isolated project.
  • .changeset/fuzzy-ravens-smile.md — patch bump with consumer-facing wording.

I verified the fix is load-bearing: reverting IntrinsicProps to the pre-fix WithCSSVars<...> form re-produces TS2590 at styled.tsx(23,33) and src/types.ts, so the new test genuinely fails without the change, and it compiles with the change in place. No acceptance regression observed in the widened style between the two paths.

ℹ️ Nitpicks

  • Two parallel widening implementations now coexist (WithCSSVars and WithCSSVarsForDataIndex), producing subtly different style types. They are behaviorally equivalent for intrinsic elements today, but they are separate surfaces to keep in sync; the comments already document the divergence.

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

Comment on lines +230 to +231
type IntrinsicElementsHaveDataIndex =
keyof DataAttributes extends keyof React.JSX.IntrinsicElements['div'] ? true : false;

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 detection probes a single HTML element (div), so the safe widening only activates when the template-literal index lives on HTMLAttributes. An app that augments SVGAttributes with the same data-* pattern still trips TS2590 on styled.svg / styled.path — I confirmed by compiling an SVGAttributes-only augmentation against this branch, which re-produces the exact styled.tsx(23,33) and src/types.ts errors. The reported issue (#5796) is HTML-only, so this is a coverage note rather than a blocker, but the PR title claims general "template index signatures" support.

…ts cost

Builds on the data-* template-literal index signature fix.

- Widen the augmented intrinsic style through a FastOmit union rather than a
  plain intersection. A plain intersection forces style to require the custom
  property index, so an explicit IStyledComponent declaration-site annotation
  carrying plain CSSProperties no longer relates (a real TS2322). The union form
  mirrors the normal WithCSSVars widening and keeps that relation intact.
- Cover the two cases the reporter flagged that the first patch left untested:
  the conditional call site (cond ? A : B) and the declaration-site annotation.
- Add a --augment mode to typePerf.mjs, with its own budget file and a CI step,
  so the data-* path's type-check cost (and this regression) is guarded rather
  than invisible. The default fixture never exercises that path.
- Document that the probe detects data-* only; other template prefixes still
  fall through to the standard widening.

@quantizor quantizor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix and the careful reproduction. I pushed a follow-up commit onto the branch: it swaps the augmented-path widening to a FastOmit union so an explicit IStyledComponent declaration-site annotation still relates (a plain intersection forces the custom-property index onto style and breaks that assignment), adds tests for the conditional call site and the annotation, and wires a --augment type-perf budget so the data-* path is guarded in CI. Verified: type tests, both perf budgets, web + native suites all green.

@quantizor
quantizor merged commit 3470387 into styled-components:6.x Aug 15, 2026
7 of 8 checks passed
quantizor pushed a commit that referenced this pull request Nov 24, 2026
…ponent (#5879)

fix(native): prevent TS2590 on Reanimated refs after a styled component

The data-* probe added by #5797 uses keyof
React.JSX.IntrinsicElements['div']. A React Native app using a styled
component will evaluate the probe, and with Reanimated's useAnimatedRef
in the code, that will cause TypeScript to show the TS2590 error on the
ref (#5877). The probe is now using keyof on each part of the props
(ClassAttributes and HTMLAttributes) separately, not on their
intersection, which means the same keys are being used. However,
checking a DOM tag or web styled.div before the ref will still result
in the TS2590 error being shown, as with a normal <div> without
styled-components.

The new type test will use a data-* index on React.Attributes for
checking the ClassAttributes part. This will fail with TS2590 when
using the probe on HTMLAttributes only. However, this passes on 6.x as
well. In-tree tests cannot fail on 6.x as the trigger requires the
React Native and Reanimated types, so #5877 has been added to the
out-of-tree list in docs/type-contracts.md.

Only the emitted .d.ts files have changed. The built JS is byte for
byte identical. These checks passed: test:types, typecheck,
prettier:check, test:types:dist, check:type-resolution. type-perf
(36,886 types, 738,335 instantiations) and type-perf:augment (37,045
types, 812,680 instantiations) also match 6.x (TypeScript version
5.9.3, @types/react version 18.3.28). test:web and test:native were
not run. test:build failed 8/36 on Windows for both base and branch
due to the backslashes being removed from the entry path by webpack.
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.

2 participants