登录

类型记录中的React事件类型

内容来源于 Stack Overflow,遵循 CC BY-SA 4.0 许可协议进行翻译与使用。IT领域专用引擎提供翻译支持

腾讯云小微IT领域专用引擎提供翻译支持

原文
Stack Overflow用户 修改于2017-10-04
  • 该问题已被编辑
  • 提问者: Stack Overflow用户
  • 提问时间: 2017-10-02 11:43

当我在事件处理程序中使用 MouseEvent 类型时,我无法构建我的React/类型记录应用程序:

private ButtonClickHandler(event: MouseEvent): void
}

我得到:

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>'.`

我也尝试过 MouseEvent<HTMLButtonElement> ,但后来我得到了 error TS2315: Type 'MouseEvent' is not generic.

为什么?

摘录自我的 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",