With Typescript, type intersection for onChange with SVGProps
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
Reproduction link
Steps to reproduce
Implement onChange method for Brush in a Typescript environment.
function App() {
var brushChange = (newIndex: BrushStartEndIndex) => {
console.log(newIndex);
};
return (
<LineChart
width={600}
height={300}
data={data}
margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
>
<Brush onChange={brushChange} />
<XAxis dataKey="name" />
...
What is expected?
This code should work.
What is actually happening?
Error in compiling
Type '(newIndex: BrushStartEndIndex) => void' is not assignable to type '((event: FormEvent<SVGElement>) => void) & ((newIndex: BrushStartEndIndex) => void)'.
The Brush props is defined as
export declare type Props = SVGProps<SVGElement> & BrushProps;
BrushProps defines onChange
but SVGProps also defines onChange
.
So the resulting Props is an intersecting onChange definition:
(JSX attribute) onChange?: (((event: React.FormEvent<SVGElement>) => void) & ((newIndex: BrushStartEndIndex) => void)) | undefined
There might be a way to specify only one type in the implementation, but I don’t know it…
Omitting the SVGProps onChange
does fix the issue (but might prevent legitimate use of it).
export declare type Props = Omit<SVGProps<SVGElement>,'onChange'> & BrushProps;
NB: It could be useful to export BrushStartEndIndex too!
Environment | Info |
---|---|
Recharts | v2.0.8 |
React | 17.0.1 |
System | nodejs 6.14.11 |
Browser | Chrome 86.0.4240 |
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:7
Top Results From Across the Web
TypeScript: Expected type comes from property 'children ...
The type ImgHTTMLAttributes<HTMLImageElement> is a type that describes a simple object, that contains all properties that img jsx element ...
Read more >Handbook - Unions and Intersection Types - TypeScript
How to use unions and intersection types in TypeScript. ... Intersection and Union types are one of the ways in which you can...
Read more >typescript-cheatsheet - GitHub Pages
A set of TypeScript related notes used for quick reference. The cheatsheet contains references to types, classes, decorators, and many other TypeScript ......
Read more >Deciphering TypeScript's React errors | by Fiona Hopkins |
& in TypeScript makes an “intersection” between two types, which is kind of like an object that has all the properties from each...
Read more >types/react definitions - UNPKG
(): P; } /** * We use an intersection type to infer multiple type parameters from ... See https://github.com/Microsoft/TypeScript/issues/7234 for more info.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Where to pass that Props then? Thank you!
You should declare the new Props anywhere in your tsx