在 addEvenListener 上reacttypescript问题

IT技术 javascript reactjs typescript
2021-05-08 09:12:31

如果我不担心typescript,我有下面的实现。

  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]);

但是document.addEventListenerdocument.addEventListener并显示typescript错误如下:

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) {

现在,如果我改变行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。

2个回答

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

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

修复:使用正确的注释

改变

const escFunction: (event: React.KeyboardEvent<Element>)

const escFunction: (event: Event)

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

所以使用:

const escFunction = (event: KeyboardEvent): void =>

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