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


92

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

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

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

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

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

在此处输入图片说明

我的软件包的版本:

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

这是为什么?

Answers:


248

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


2
不为我工作,我得到:type: "checkbox" onChange: (e: MouseEvent<HTMLInputElement>) => void; defaultChec...' is not assignable to type 'InputHTMLAttributes<HTMLInputElement>'
Kokodoko

@Kokodoko您是否解决了这个问题?
SuperUberDuper '17


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.