我正在写一个反应应用程序的类型记录,在那里,我试图处理右击和左键。
这是我的界面
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 | />我对打字稿不太了解,但据我说,HTMLDivElement应该是HTMLElement类型的,对吗?
发布于 2020-03-01 04:23:53
解决方案
从HTMLDivElement改为Element解决您的问题。
// 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的道具。
https://stackoverflow.com/questions/60471034
复制相似问题