首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键入'MouseEvent<Element,MouseEvent>‘不能指定键入'MouseEvent<HTMLDivElement,MouseEvent>’

键入'MouseEvent<Element,MouseEvent>‘不能指定键入'MouseEvent<HTMLDivElement,MouseEvent>’
EN

Stack Overflow用户
提问于 2020-03-01 00:51:06
回答 1查看 18.6K关注 0票数 16

我正在写一个反应应用程序的类型记录,在那里,我试图处理右击和左键。

这是我的界面

代码语言:javascript
复制
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;
} 

现在,我声明了回调函数,如

代码语言:javascript
复制
const handleContextMenu = (rowParam: number, cellParam: number) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) : void => {
      e.preventDefault();
}

最后声明了我的组件

代码语言:javascript
复制
<Button 
  key={`${rowIndex}*${cellIndex}`} 
  value={cell.value} 
  state={cell.state} 
  onClick={handleCellClick}
  onContext={handleContextMenu}
  row={rowIndex} 
  col={cellIndex} 
/>

但我犯了个错误

代码语言:javascript
复制
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 |             />

我对打字稿不太了解,但据我说,HTMLDivElement应该是HTMLElement类型的,对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-01 04:23:53

解决方案

HTMLDivElement改为Element解决您的问题。

代码语言:javascript
复制
// Before
const handleContextMenu = (...) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) : void => {
  ...
}
// After
const handleContextMenu = (...) => (e: React.MouseEvent<Element, MouseEvent>) : void => {
  ...
}

解释

层次关系如下:

⌞元素

⌞高温超导元件

⌞高温分解元件

错误消息显示的内容如下:

类型‘元素’缺少‘HTMLDivElement’类型中的下列属性:对齐、accessKey、accessKeyLabel、自动大写和111个。TS2322

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

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60471034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档