Repository navigation
fix(native): avoid TS2590 error on Reanimated refs after a styled component - #5879
Conversation
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 detectedLatest commit: fdafa44 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 |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Probe rewrite —
IntrinsicElementsHaveDataIndexnow keysReact.ClassAttributes<HTMLElement> | React.HTMLAttributes<HTMLElement>instead ofReact.JSX.IntrinsicElements['div'], so evaluating it no longer resolves every intrinsic element's prop bag. - New contract fixture —
test-types/attributes-index-signature.tsxplus its own tsconfig cover adata-*index onReact.Attributes, wired intotest:typesandcheck: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.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
|
Thanks @hnwg, clean fix and great type contract coverage. Merging this into 6.x and forward-porting to main. |

Since v6.5.3,
tscthrows a TS2590 error ("Expression produces a union type that is too complex to represent") when using a styled component fromstyled-components/nativeand theuseAnimatedReffrom Reanimated. This error appears on the ref, in something like auseCallbackdependency 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 errorsrc/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.divrendered 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 eitherReact.HTMLAttributesorReact.Attributeswill type-check the same as on6.x.Fixes #5877