【问题标题】:Why 'input' event has `Event` type of event instead of `InputEvent`为什么“输入”事件具有“事件”类型的事件而不是“输入事件”
【发布时间】:2020-11-26 02:58:23
【问题描述】:

您好,我想知道为什么 'input' 事件类型的类型是 Event 类型(我认为应该是 InputEvent)?

我在这样做时遇到了这个问题:

interface OnInputTextFieldListenerProps extends BaseTextFieldListenerProps {
    eventName: Extract<keyof GlobalEventHandlersEventMap, 'input'>;
    callback(e?: InputEvent): void;
}

if (isOnInputTextFieldProps(listenerProps)) {
    this.input.addEventListener<'input'>(listenerProps.eventName, listenerProps.callback.bind(this));
}

现在我收到了这个错误:

TS2345:'(e?: InputEvent | undefined) => void' 类型的参数不可分配给'(this: HTMLInputElement, ev: Event) => any' 类型的参数。

这是完全正确的。但是为什么GlobalEventHandlersEventMap 'input' 被输入为Event 而不是InputEvent

您可以在 lib.dom.d.ts5715 行找到这个GlobalEventHandlersEventMap 接口。

注意

MDN documentation 表示GlobalEventHandlers.oninput 接口是InputEventoninput 可能相当于打字稿 input。我是这么认为的,因为在 MDN 文档中,GlobalEventHandlers 接口的每个键都以 on 开头。

我还决定在 Typescript 存储库中打开一个问题,您可以在这里找到:Issue - 39925

【问题讨论】:

    标签: javascript typescript dom types


    【解决方案1】:

    所以这个'input'事件是Event而不是InputEvent的主要原因是InputEventdraft specification的一部分不是official HTML living standard

    如果您想了解更多信息,可以阅读 Andrew Branch

    提供的 the whole answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2020-03-31
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多