Skip to content

fix(native): avoid TS2590 error on Reanimated refs after a styled component - #5879

Merged
quantizor merged 1 commit into
styled-components:6.xfrom
hnwg:fix-native-probe-ts2590
Nov 24, 2026
Merged

quantizor merged 1 commit into
styled-components:6.xfrom
hnwg:fix-native-probe-ts2590

Conversation

@hnwg

@hnwg hnwg commented Nov 16, 2026

Copy link
Copy Markdown

Since v6.5.3, tsc throws a TS2590 error ("Expression produces a union type that is too complex to represent") when using a styled component from styled-components/native and the useAnimatedRef from Reanimated. This error appears on the ref, in something like a useCallback dependency list, in a file that doesn't import any styled components.

Using the standalone repro from the issue ([email protected], [email protected], @types/[email protected], [email protected]) we can see that both a published version of v6.6.0 and a built version of 6.x (f17f6c0) throw the error src/useScroll.ts(8,6): error TS2590. However, a built version of this branch does not give any errors in either file order. v6.5.2 does not give any errors.

If you are rendering a DOM element before TypeScript checks the ref, then this won't help. In the standalone repro, a web styled.div rendered first will still throw the TS2590 error with a built version of this branch. Even with a standard <div /> the error will appear, even when the program has not imported styled-components. This is due to the Reanimated and TypeScript types.

Apps that add a data-* index signature to either React.HTMLAttributes or React.Attributes will type-check the same as on 6.x.

Fixes #5877

The data-* probe added by styled-components#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 (styled-components#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 styled-components#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.
@changeset-bot

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fdafa44

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

  • Probe rewrite — IntrinsicElementsHaveDataIndex now keys React.ClassAttributes<HTMLElement> | React.HTMLAttributes<HTMLElement> instead of React.JSX.IntrinsicElements['div'], so evaluating it no longer resolves every intrinsic element's prop bag.
  • New contract fixture — test-types/attributes-index-signature.tsx plus its own tsconfig cover a data-* index on React.Attributes, wired into test:types and check:type-resolution.
  • Docs and changeset — #5877 joins the "verified out of tree" list in docs/type-contracts.md; the patch changeset states the boundary (a web/DOM check ordered first can still surface TS2590, as it can without styled-components).

The swap is semantics-preserving, not just close: IntrinsicElements['div'] is ClassAttributes<HTMLDivElement> & HTMLAttributes<HTMLDivElement>, so the old keyof was already exactly the union of the two key sets the new probe spells out (the element type argument does not affect keys). All four type projects and the source type-check pass at head, and the new fixture fails with TS2590 at src/types.ts when the probe keys HTMLAttributes alone, matching its docblock, so it genuinely pins the ClassAttributes half.

ℹ️ GitHub took this as a comment because it requires explicit repository access to approve or block a pull request here.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@quantizor

Copy link
Copy Markdown
Contributor

Thanks @hnwg, clean fix and great type contract coverage. Merging this into 6.x and forward-porting to main.

@quantizor
quantizor merged commit 4eaf66b into styled-components:6.x Nov 24, 2026
8 checks passed
quantizor added a commit that referenced this pull request Nov 24, 2026
…ponent (#5879) (#5889)

* fix(native): avoid TS2590 error on Reanimated refs after a styled component (#5879)

* fix(types): use react-jsx in attributes-index fixture and expand assertions

* docs(test-types): document react-jsx override in attributes-index fixture
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