窗口上的 TypeScript mousemove 事件

axt*_*tck 8 events mouseevent typescript reactjs

我正在尝试使用 React 和 TypeScript 在控制台中mousemove记录事件。window经过一些研究并查看类似的问题后,我认为我可以用作MouseEvent传递给侦听器的事件的类型。

useEffect(() => {
  const onMouseMove = (e: MouseEvent) => {
    e.preventDefault();
    console.log(e);
  };

  window.addEventListener("mousemove", onMouseMove);

  return () => {
    window.removeEventListener("mousemove", onMouseMove);
  };
}, []);
Run Code Online (Sandbox Code Playgroud)

但运行时提示我以下错误:

const onMouseMove: (e: MouseEvent) => void
No overload matches this call.
  Overload 1 of 2, '(type: "mousemove", listener: (this: Window, ev: MouseEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(e: MouseEvent) => void' is not assignable to parameter of type '(this: Window, ev: MouseEvent) => any'.
      Types of parameters 'e' and 'ev' are incompatible.
        Type 'MouseEvent' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': nativeEvent, isDefaultPrevented, isPropagationStopped, persist
  Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(e: MouseEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
      Type '(e: MouseEvent) => void' is not assignable to type 'EventListener'.
        Types of parameters 'e' and 'evt' are incompatible.
          Type 'Event' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': altKey, button, buttons, clientX, and 18 more
Run Code Online (Sandbox Code Playgroud)

我尝试使用一些建议更新我的事件侦听器语法,但它们似乎都无法解决问题。

const onMouseMove: (e: MouseEvent) => void
No overload matches this call.
  Overload 1 of 2, '(type: "mousemove", listener: (this: Window, ev: MouseEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(e: MouseEvent) => void' is not assignable to parameter of type '(this: Window, ev: MouseEvent) => any'.
      Types of parameters 'e' and 'ev' are incompatible.
        Type 'MouseEvent' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': nativeEvent, isDefaultPrevented, isPropagationStopped, persist
  Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(e: MouseEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
      Type '(e: MouseEvent) => void' is not assignable to type 'EventListener'.
        Types of parameters 'e' and 'evt' are incompatible.
          Type 'Event' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': altKey, button, buttons, clientX, and 18 more
Run Code Online (Sandbox Code Playgroud)

som*_*llg 17

请注意,React 有自己的 MouseEvent 定义(以及其他事件)

当您向 React Element 添加事件处理程序时,您必须使用 React Event

<div onClick={(e: React.MouseEvent) => { console.log(e) }></div>
Run Code Online (Sandbox Code Playgroud)

但是如果您使用 DOM API 添加事件监听器,请确保它来自 DOM

  // DOM Event not to be confused with React.MouseEvent
  const onMouseMove = (e: MouseEvent) => {
    e.preventDefault();
    console.log(e);
  };

  window.addEventListener("mousemove", onMouseMove);

  return () => {
    window.removeEventListener("mousemove", onMouseMove);
  };
Run Code Online (Sandbox Code Playgroud)

TypeScript 支持盒子的 DOM 事件。如果您从反应导入事件

import { MouseEvent } from 'react';
Run Code Online (Sandbox Code Playgroud)

这将覆盖 DOM 的 MouseEvent,而 TypeScript 会不满意,因为这两种类型是不同的

  • 这是关键,谢谢!VSCode 自动从 React 导入 MouseEvent,这是我的问题。需要使用全局DOM MouseEvent类型,无需导入! (2认同)