Search code examples
javascripttypescriptdomtypesinputevent

Why 'input' event has `Event` type of event instead of `InputEvent`


Hi I am wondering why type of 'input' event type is a Event type (I thought it should be InputEvent)?

I came across on this problem doing this:

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

And now i am getting this error:

TS2345: Argument of type '(e?: InputEvent | undefined) => void' is not assignable to parameter of type '(this: HTMLInputElement, ev: Event) => any'.

Which is fully correct. But why GlobalEventHandlersEventMap 'input' is typed as Event instead of InputEvent.

You can find this GlobalEventHandlersEventMap interface here lib.dom.d.ts at line 5715.

Note

MDN documentation says that GlobalEventHandlers.oninput interface is InputEvent. oninput is probalby equivalent of typescript input. I think like that because in MDN docs every key of GlobalEventHandlers interface starts with on.

I also decided to open an issue in Typescript repo which you can find here: Issue - 39925


Solution

  • So the main reason of why this 'input' event is type of Event instead of InputEvent is that InputEvent is a part of draft specification NOT official HTML living standard.

    If you want to learn more you can read the whole answer provided by Andrew Branch