typescript 为什么复选框事件处理程序中的“MouseEvent”不是通用的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44764146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:40:11  来源:igfitidea点击:

Why is the `MouseEvent` in the checkbox event handler not generic?

reactjstypescripteventstypesjsx

提问by Ben

I have an checkbox TSX(JSX) element:

我有一个复选框 TSX(JSX) 元素:

<input type="checkbox" name={i.toString()} onClick={this.handleCheckboxClick} />

With the help of VS code I know that the input parameter type of the this.handleCheckboxClickis MouseEvent<HTMLInputElement>. So I implemented it with:

随着VS代码的帮助,我知道,输入参数类型this.handleCheckboxClickMouseEvent<HTMLInputElement>。所以我实现了它:

private handleCheckboxClick(event: MouseEvent<HTMLInputElement>) {
    ...
}

Then I get an error saying [ts] Type 'MouseEvent' is not generic.As shown in the image below:

然后我收到一条错误消息[ts] Type 'MouseEvent' is not generic.,如下图所示:

enter image description here

在此处输入图片说明

Version of my packages:

我的软件包版本:

"@types/react": "^15.0.29",
"@types/react-dom": "^15.5.0",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"typescript": "^2.3.4",

Why is that?

这是为什么?

回答by rossipedia

You're probably using the DOM MouseEvent. Try using React.MouseEvent<HTMLInputElement>instead.

您可能正在使用 DOM MouseEvent。尝试使用React.MouseEvent<HTMLInputElement>

回答by Guy Engel

In order to use React MouseEvent just make sure to add:

为了使用 React MouseEvent 只需确保添加:

import { MouseEvent } from 'react';

回答by Sras

You can use SyntheticEvent instead of MouseEvent

您可以使用 SyntheticEvent 而不是 MouseEvent