在 addEvenListener 上反应打字稿问题

Vin*_*ati 0 javascript typescript reactjs

如果我不担心打字稿,我有下面的实现。

  const escFunction: (event: React.KeyboardEvent<Element>) => any = useCallback((event: React.KeyboardEvent["keydown"]) => {
    if (event.keyCode === KEY_CODE.ESC) {
      closeFn();
    }
  }, [closeFn]);

  if (closeTimeout) {
    setTimeout(closeFn, closeTimeout);
  }


  useEffect(() => {
    if (shouldCloseOnEsc) {
      document.addEventListener(KEY_EVENT.KEYDOWN, escFunction, false);
    }
    return () => {
      if (shouldCloseOnEsc) {
        document.removeEventListener(KEY_EVENT.KEYDOWN, escFunction, false);
      }
    }
  }, [shouldCloseOnEsc, escFunction]);
Run Code Online (Sandbox Code Playgroud)

但是document.addEventListenerdocument.addEventListener并显示打字稿错误如下:

TypeScript error in /mnt/d/Projects/sb-modal/src/components/modal/Modal.tsx(63,7):
No overload matches this call.
  Overload 1 of 2, '(type: "input" | "progress" | "select" | "cancel" | "keydown" | "fullscreenchange" | "fullscreenerror" | "pointerlockchange" | "pointerlockerror" | "readystatechange" | "visibilitychange" | ... 85 more ... | "paste", listener: (this: Document, ev: MouseEvent | ... 14 more ... | ClipboardEvent) => any, options?: boolean | AddEventListenerOptions): void', gave the following error.
    Argument of type 'string' is not assignable to parameter of type '"input" | "progress" | "select" | "cancel" | "keydown" | "fullscreenchange" | "fullscreenerror" | "pointerlockchange" | "pointerlockerror" | "readystatechange" | "visibilitychange" | ... 85 more ... | "paste"'.
  Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void', gave the following error.
    Argument of type '(event: KeyboardEvent<Element>) => any' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
      Type '(event: KeyboardEvent<Element>) => any' is not assignable to type 'EventListener'.
        Types of parameters 'event' and 'evt' are incompatible.
          Type 'Event' is missing the following properties from type 'KeyboardEvent<Element>': altKey, charCode, ctrlKey, getModifierState, and 12 more.  TS2769

    61 |   useEffect(() => {
    62 |     if (shouldCloseOnEsc) {
  > 63 |       document.addEventListener(KEY_EVENT.KEYDOWN, escFunction, false);
       |       ^
    64 |     }
    65 |     return () => {
    66 |       if (shouldCloseOnEsc) {
Run Code Online (Sandbox Code Playgroud)

现在,如果我改变行const escFunction: (event: React.KeyboardEvent<Element>) => any = useCallback((event: React.KeyboardEvent<Element>) => {const escFunction: any = useCallback((event: React.KeyboardEvent<Element>) => {它编译得很好。

我正在使用 react - 16.8.x create-react-app install。

Mat*_*abe 6

addEventListener('keydown'...将开火KeyboardEvent

所以使用:

const escFunction = (event: KeyboardEvent): void =>
Run Code Online (Sandbox Code Playgroud)

Event将部分工作,但会缺少您可能需要访问的一些属性,例如.keyCode.key


bas*_*rat 5

您在 document 上有一个事件侦听器: document.addEventListener(KEY_EVENT.KEYDOWN, escFunction, false);

document.addEventListener会调用回调(在这种情况下escFunction),与原生键盘事件。但是,您已将回调escFunction注释为接受React键盘事件。

修复:使用正确的注释

改变

const escFunction: (event: React.KeyboardEvent<Element>)
Run Code Online (Sandbox Code Playgroud)

const escFunction: (event: Event)
Run Code Online (Sandbox Code Playgroud)

  • 在更改为“const escFunction: (event: Event) =&gt; any = useCallback((event: KeyboardEvent): void =&gt; {`”时,我收到错误“类型‘Event’缺少类型‘KeyboardEvent’的以下属性” (2认同)