相关文章推荐
慷慨大方的薯片  ·  react ...·  1 月前    · 
潇洒的伤疤  ·  設定 CORS Proxy ...·  1 周前    · 
瘦瘦的伤痕  ·  javascript - ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Type 'MouseEvent<Element, MouseEvent>' is not assignable to type 'MouseEvent<HTMLDivElement, MouseEvent>'

Ask Question

I am writing a react application in typescript where I am trying to handle both right click and left click.

This is my interface

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;

Now I have declared the callback function like

const handleContextMenu = (rowParam: number, cellParam: number) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) : void => {
      e.preventDefault();

and finally declared my component like

<Button 
  key={`${rowIndex}*${cellIndex}`} 
  value={cell.value} 
  state={cell.state} 
  onClick={handleCellClick}
  onContext={handleContextMenu}
  row={rowIndex} 
  col={cellIndex} 

But I get an error

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 |             />

I don't know much about typescript but according to me HTMLDivElement should be of type HTMLElement right?

I don't know much about typescript either, but it seems to me that the parameter on both onclick and onContext should be the event object. You have some (rowParam: number, colParam: number) params – i.brod Mar 1, 2020 at 1:36

Change from HTMLDivElement to Element solve your problem.

// Before
const handleContextMenu = (...) => (e: React.MouseEvent<HTMLDivElement, MouseEvent>) : void => {
// After
const handleContextMenu = (...) => (e: React.MouseEvent<Element, MouseEvent>) : void => {

Explanation

The hierarchy relationship is as below:

⌞Element
  ⌞HTMLElement
    ⌞HTMLDivElement

  • ref: Interface HTMLDivElement
  • The error message is showing something like:

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

    This is telling that there are some props that belong to Element could not be found in HTMLDivElement.

    It happened to me that I did not use React.MouseEvent, nor did I import MouseEvent explicitly from 'react'. Then TS screams that MouseEvent is not generic (some other interface). – BairDev Sep 1, 2022 at 15:36

    So, for those people who are getting

    Type 'MouseEvent' is not generic.

    Make sure you are importing from React

    import { MouseEvent } from 'react';

    React.MouseEvent<HTMLButtonElement>

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.