Skip to content

Commit 4eaf66b

Browse files
authored
fix(native): avoid TS2590 error on Reanimated refs after a styled component (#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.
1 parent f17f6c0 commit 4eaf66b

7 files changed

Lines changed: 52 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'styled-components': patch
3+
---
4+
5+
A `styled-components/native` component will no longer cause TypeScript to show an error of TS2590 ("Expression produces a union type that is too complex to represent") on a Reanimated `useAnimatedRef` ref, which it did from 6.5.3. However, if a DOM element or web styled component is checked earlier in the code then this error can still show, like a normal `<div>` would without styled-components.

‎docs/type-contracts.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ add the real package as a dependency to test a type.
4848

4949
## Verified out of tree
5050

51-
Four reported issues cannot be pinned in-tree, because each is a union-explosion or recursion-depth
51+
Five reported issues cannot be pinned in-tree, because each is a union-explosion or recursion-depth
5252
cliff that a simplified stub sits clear of and would pass vacuously:
5353

5454
- antd's `Button` under `.attrs()` (#5725)
5555
- react-bootstrap 2.8's `Button` (#4166)
5656
- react-spring's `animated` with the `css` prop (#3496)
5757
- preact/compat (#3773)
58+
- react-native-reanimated's `useAnimatedRef` next to a native styled component (#5877)
5859

5960
They are verified out of tree against the real packages at the versions reported. Re-verify them by
6061
hand when the polymorphic call signature or `Interpolation` changes.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"private": true,
44
"scripts": {
55
"build": "pnpm --filter styled-components build",
6-
"check:type-resolution": "node scripts/check-type-resolution.mjs packages/styled-components/tsconfig.json packages/styled-components/tsconfig.test-types.json packages/styled-components/tsconfig.test-template-index.json packages/styled-components/tsconfig.test-intrinsic-index-signature.json packages/sandbox/tsconfig.json",
6+
"check:type-resolution": "node scripts/check-type-resolution.mjs packages/styled-components/tsconfig.json packages/styled-components/tsconfig.test-types.json packages/styled-components/tsconfig.test-template-index.json packages/styled-components/tsconfig.test-attributes-index.json packages/styled-components/tsconfig.test-intrinsic-index-signature.json packages/sandbox/tsconfig.json",
77
"sandbox": "pnpm --filter sandbox dev",
88
"release": "pnpm --filter styled-components prepublishOnly && changeset publish",
99
"postinstall": "pnpm --filter styled-components generateErrors",

‎packages/styled-components/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"test:build": "jest -c jest.config.build.js",
2424
"pretest": "pnpm run generateErrors",
2525
"test": "pnpm run test:web && pnpm run test:native && pnpm run test:types",
26-
"test:types": "tsc --noEmit --project tsconfig.test-types.json && tsc --noEmit --project tsconfig.test-template-index.json && tsc --noEmit --project tsconfig.test-intrinsic-index-signature.json",
26+
"test:types": "tsc --noEmit --project tsconfig.test-types.json && tsc --noEmit --project tsconfig.test-template-index.json && tsc --noEmit --project tsconfig.test-attributes-index.json && tsc --noEmit --project tsconfig.test-intrinsic-index-signature.json",
2727
"test:types:dist": "node scripts/typeCheckDist.mjs",
2828
"type-perf": "node scripts/typePerf.mjs",
2929
"type-perf:augment": "node scripts/typePerf.mjs --augment",

‎packages/styled-components/src/types.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,24 @@ export type TargetProps<R extends Runtime, T> = T extends keyof React.JSX.Intrin
221221
: {};
222222

223223
/**
224-
* True when an application has augmented `React.HTMLAttributes` with a `data-*`
225-
* template-literal index signature, the common pattern for allowing arbitrary
226-
* data attributes. Detected by testing whether {@link DataAttributes}' key is
227-
* already a key of a stock intrinsic element.
224+
* True when an application has augmented `React.HTMLAttributes` or
225+
* `React.Attributes` with a `data-*` template-literal index signature, the
226+
* common pattern for allowing arbitrary data attributes. Detected by testing
227+
* whether {@link DataAttributes}' key is already a key of a stock intrinsic
228+
* element's `ClassAttributes` or `HTMLAttributes`. Keying the whole
229+
* `React.JSX.IntrinsicElements['div']` instead makes TypeScript report TS2590
230+
* at Reanimated animated refs in React Native apps (#5877).
228231
*
229232
* Scope is exactly `data-${string}`: an `aria-${string}` or other custom-prefix
230233
* template augmentation is not detected and still hits the {@link WithCSSVars}
231234
* path (#5796). `data-*` is the dominant augmentation, so the narrow probe buys
232235
* the common case; widening it means OR-ing another literal prefix in here.
233236
*/
234-
type IntrinsicElementsHaveDataIndex =
235-
keyof DataAttributes extends keyof React.JSX.IntrinsicElements['div'] ? true : false;
237+
type IntrinsicElementsHaveDataIndex = keyof DataAttributes extends
238+
| keyof React.ClassAttributes<HTMLElement>
239+
| keyof React.HTMLAttributes<HTMLElement>
240+
? true
241+
: false;
236242

237243
/**
238244
* {@link WithCSSVars} for the augmented intrinsic path. Identical widening, but
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import styled from '../src';
3+
4+
/**
5+
* A `data-*` template-literal index on `React.Attributes` reaches every
6+
* intrinsic element through its ref and key half. A probe that keys
7+
* `HTMLAttributes` alone misses it and reports TS2590, as in #5796.
8+
*/
9+
declare module 'react' {
10+
interface Attributes {
11+
[dataAttribute: `data-${string}`]: string | undefined;
12+
}
13+
}
14+
15+
const Example = styled.div``;
16+
17+
<Example data-role="test" style={{ '--accent': 'tomato' }} />;
18+
19+
// @ts-expect-error an unknown prop is still rejected under the augmentation
20+
<Example thisPropDoesNotExist="nope" />;
21+
22+
// @ts-expect-error data attribute values retain the augmented string type
23+
const invalidAttrs: React.Attributes = { 'data-role': 42 };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"incremental": false,
5+
"noUnusedLocals": false
6+
},
7+
"include": ["test-types/attributes-index-signature.tsx"]
8+
}

0 commit comments

Comments
 (0)