question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

Edit on CodeSandbox

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:open
  • Created 3 years ago
  • Reactions:9
  • Comments:7

github_iconTop GitHub Comments

1reaction
natBizitzacommented, Aug 3, 2022
 export declare type Props = Omit<SVGProps<SVGElement>,'onChange'> & BrushProps;

Where to pass that Props then? Thank you!

1reaction
raomincommented, Jul 28, 2022

You should declare the new Props anywhere in your tsx

 export declare type Props = Omit<SVGProps<SVGElement>,'onChange'> & BrushProps;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found