I can't build my React/Typescript app when I use MouseEvent type in my event handler:
private ButtonClickHandler(event: MouseEvent): void
{
...
}
I get:
error TS2322: Type '{ onClick: (event: MouseEvent) => void; children: string; }' is not assignable to type 'DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>'.
Type '{ onClick: (event: MouseEvent) => void; children: string; }' is not assignable to type 'ButtonHTMLAttributes<HTMLButtonElement>'.
Types of property 'onClick' are incompatible.
Type '(event: MouseEvent) => void' is not assignable to type 'EventHandler<MouseEvent<HTMLButtonElement>>'.
Types of parameters 'event' and 'event' are incompatible.
Type 'MouseEvent<HTMLButtonElement>' is not assignable to type 'MouseEvent'.
Property 'fromElement' is missing in type 'MouseEvent<HTMLButtonElement>'.`
I also tried MouseEvent<HTMLButtonElement> but then I got error TS2315: Type 'MouseEvent' is not generic..
Why?
Excerpt from my package.json:
"devDependencies": {
"@types/react": "^16.0.7",
"@types/react-dom": "^15.5.5",
"ts-loader": "^2.3.7",
"typescript": "^2.5.3",
"webpack": "^3.6.0"
},
"dependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0"
}
Edit #1:
Binding in render():
<button onClick={ this.ButtonClickHandler }>Toggle</button>
in constructor:
this.ButtonClickHandler = this.ButtonClickHandler.bind(this);
(event: React.MouseEvent<HTMLButtonElement>)?private ButtonClickHandler(event: React.MouseEvent<HtmlButtonElement>): voidshould work