reactjs - 'MouseEvent<Element, MouseEvent>' 类型不可分配给 'MouseEvent<HTMLDivElement, MouseEvent>' 类型

标签 reactjs typescript

我正在用 typescript 编写一个 react 应用程序,我试图同时处理右键单击和左键单击。

这是我的界面

interface ButtonProps {
    value: CellValue;
    state: CellState;
    row: number;
    col: number;
    onClick(rowParam: number, colParam: number) : (e: React.MouseEvent) => void;
    onContext(rowParam: number, colParam: number) : (e: React.MouseEvent) => void;
} 

现在我已经声明了回调函数
const handleContextMenu = (rowParam: number, cellParam: number) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) : void => {
      e.preventDefault();
}

最后声明了我的组件
<Button 
  key={`${rowIndex}*${cellIndex}`} 
  value={cell.value} 
  state={cell.state} 
  onClick={handleCellClick}
  onContext={handleContextMenu}
  row={rowIndex} 
  col={cellIndex} 
/>

但我收到一个错误
Type '(rowParam: number, cellParam: number) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void' is not assignable to type '(rowParam: number, colParam: number) => (e: MouseEvent<Element, MouseEvent>) => void'.
  Type '(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void' is not assignable to type '(e: MouseEvent<Element, MouseEvent>) => void'.
    Types of parameters 'e' and 'e' are incompatible.
      Type 'MouseEvent<Element, MouseEvent>' is not assignable to type 'MouseEvent<HTMLDivElement, MouseEvent>'.
        Type 'Element' is missing the following properties from type 'HTMLDivElement': align, accessKey, accessKeyLabel, autocapitalize, and 111 more.  TS2322

    53 |                 state={cell.state} 
    54 |                 onClick={handleCellClick}
  > 55 |                 onContext={handleContextMenu}
       |                 ^
    56 |                 row={rowIndex} 
    57 |                 col={cellIndex} 
    58 |             />

我不太了解 typescript ,但据我说 HTMLDivElement 应该是 HTMLElement 类型,对吗?

最佳答案

解决方案
HTMLDivElement 更改为 元素 解决您的问题。

// Before
const handleContextMenu = (...) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) : void => {
  ...
}
// After
const handleContextMenu = (...) => (e: React.MouseEvent<Element, MouseEvent>) : void => {
  ...
}
解释
层级关系如下:
⌞元素
⌞HTML元素
⌞HTMLDiv元素
  • 引用:Interface HTMLDivElement

  • 错误消息显示如下:

    Type 'Element' is missing the following properties from type 'HTMLDivElement': align, accessKey, accessKeyLabel, autocapitalize, and 111 more. TS2322


    这说明在 Element 中找不到一些属于 HTMLDivElement 的 Prop 。

    关于reactjs - 'MouseEvent<Element, MouseEvent>' 类型不可分配给 'MouseEvent<HTMLDivElement, MouseEvent>' 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60471034/

    相关文章:

    javascript - javascript中的傅立叶变换可视化

    typescript - 如何使用 tslint 对整个文件夹进行 lint

    javascript - 如何从图像创建/保存裁剪后的图像? (将图像转换为base64)

    android - react native 自定义字体

    javascript - react Hook : Parent component not updating when dispatch is called in a child component

    javascript - 自定义rc-time-picker的样式

    typescript - 与 --build 开关一起使用时,tsc typescript 3.5 中的未知构建选项 '-p'

    reactjs - 在 React 中动态渲染另一个组件内的组件

    typescript - 扩展 vuetify VTextField

    Angular 模块从 es6 到 commonJs 结果