在addEvenListener上反应打字稿问题

时间:2019-09-30 05:50:47

标签: javascript reactjs 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 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安装。

2 个答案:

答案 0 :(得分:2)

您在文档nginx.conf上有一个事件侦听器

echo "http {server { location / {# Set path root /var/www/; try_files $uri /index.html; } }}" 将使用 native 键盘事件调用回调(在本例中为document.addEventListener(KEY_EVENT.KEYDOWN, escFunction, false);)。但是,您已将回调document.addEventListener注释为接受 React 键盘事件。

修复:使用正确的注释

更改

escFunction

收件人

escFunction

答案 1 :(得分:0)

addEventListener('keydown'...将触发KeyboardEvent

因此请使用:

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

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