recharts: Layer: TypeScript error with latest `@types/react` version
- I have searched the issues of this repository and believe that this is not a duplicate.
Reproduction link
https://stackblitz.com/edit/vitejs-vite-raiemq?file=package.json,src%2FApp.tsx
Steps to reproduce
When building recharts without skipLibCheck: true a TypeScript error is thrown with v18.2.74 of @types/react.
I believe the root cause is how TypeScript infers and represents types for forwardRef, but unfortunately I wasn’t able to fix it myself.
- Go to StackBlitz
- run
tscin terminal - See the following error:
node_modules/recharts/types/container/Layer.d.ts:7:108 - error TS2344: Type '"string" | "filter" | "values" | "fill" | "stroke" | "max" | "type" | "accumulate" | "offset" | "key" | "id" | "media" | "origin" | "height" | "width" | "end" | "name" | "min" | "alignmentBaseline" | ... 450 more ... | "onPointerLeaveCapture"' does not satisfy the constraint '"string" | "filter" | "values" | "fill" | "stroke" | "max" | "type" | "accumulate" | "offset" | "key" | "id" | "media" | "origin" | "height" | "width" | "end" | "name" | "min" | "alignmentBaseline" | ... 457 more ... | "onTransitionEndCapture"'.
Type '"onPointerEnterCapture"' is not assignable to type '"string" | "filter" | "values" | "fill" | "stroke" | "max" | "type" | "accumulate" | "offset" | "key" | "id" | "media" | "origin" | "height" | "width" | "end" | "name" | "min" | "alignmentBaseline" | ... 457 more ... | "onTransitionEndCapture"'. Did you mean '"onPointerOverCapture"'?
7 export declare const Layer: React.ForwardRefExoticComponent<Pick<React.SVGProps<SVGGElement> & LayerProps, "string" | "ideographic" | "alphabetic" | "hanging" ...[shortened for readability]> & React.RefAttributes<unknown>>;
What is expected?
Types for React v18 should be supported.
What is actually happening?
Error is thrown.
| Environment | Info |
|---|---|
| Recharts | v2.12.3 |
| React | ^18.2.0 |
| System | macOS 14.3.1 (23D60) |
| Browser | - |
"@types/react": "18.2.74"
About this issue
- Original URL
- State: closed
- Created 3 months ago
- Reactions: 2
- Comments: 19 (10 by maintainers)
Looks like the
xyzCaptureevents were removed in react types in 18.x because they don’t do anything.Relevant react issue https://github.com/facebook/react/issues/17883
Our generated declaration files include these removed events which breaks you with skipLibCheck: true.
We’ll upgrade react types in 3.x and this shouldn’t be an issue anymore, but probably won’t do it in 2.x for risk of breaking something else.
Thanks!
CC: @PavelVanecek
Fixed in https://github.com/recharts/recharts/releases/tag/v2.12.5
Lmk if this looks okay https://github.com/recharts/recharts/pull/4413 (fixing some related types while I’m at it, not sure why Pie was an HTMLElement…)
Yeah that makes sense @ckifer, thanks for taking a look at this. I’ve had a quick look and here’s my thinking. The SVGProps interface…
… is
SVGAttributesanyway withClassAttributes, which is… so the
refandkeywhich aren’t part ofSVGAttributesneed to be added, and are added implicitly fromcreateElement. and you can see thatAttributesandClassAttributesare added anyway, which is whykeyandrefare always available in your IDE even when you don’t specify it.Thanks @ckifer!
I did a bit more digging and came to the following result, maybe it helps:
Typically, type discrepancies shouldn’t be an issue, as long as only the imported types are used, allowing consumers to define the version of, e.g.
@types/reacton their end. In this case, it appears that theforwardReftypes are transformed to leverage theSVGProps'keys for thePickutility type. Consequently, these keys are bundled in with the@types/reactversion used in this project. If a type fromSVGProps(or any interface it extends) is removed, the described error is thrown.