Event Types
Event types are the TypeScript interfaces for platform and framework events: MouseEvent, KeyboardEvent, InputEvent, and React's ChangeEvent<T>. The listener's parameter type comes from the event name.
Detailed Explanation
In the DOM, addEventListener is generic over the event name. HTMLElementEventMap maps "click" to MouseEvent, "keydown" to KeyboardEvent, "input" to InputEvent. The checker uses that map when the name is a string literal. A variable const name: string = "click" loses the map and you get Event.
Event is the base. It has target typed as EventTarget | null, which does not have .value. That is the usual pain. currentTarget is the element you attached the listener to and can be typed more precisely if you attach through a typed wrapper. target is the origin of the event and may be a child. Narrow with instanceof HTMLInputElement before reading .value.
React wraps the DOM event in a synthetic event. React.MouseEvent<HTMLButtonElement> has currentTarget as HTMLButtonElement. React.ChangeEvent<HTMLInputElement> has target as HTMLInputElement. Use those in React handlers, not the DOM types, or you will fight preventDefault and pooling (pooling is gone in recent React, the types still differ).
Custom events: CustomEvent<T> has detail: T. addEventListener("my-event", ...) will not know T unless you augment WindowEventMap or HTMLElementEventMap with that name. Augment in a .d.ts so the name is a literal key.
onmessage on workers is MessageEvent<T> with T defaulting to any. See typing workers.
Do not type a handler as (e: any). Do not type it as (e: Event) and then as HTMLInputElement on e.target without a check. The instanceof is the type guard the DOM already gives you.
For bubbling and capturing as behavior, see the JavaScript events pages. This page is the maps and the target vs currentTarget types.
Code Examples
1. Literal event names pick the map entry.
// 1. Literal event names pick the map entry.
function onClick(ev: MouseEvent): void {
console.log(ev.clientX);
}
document.addEventListener("click", onClick);2. Narrow target before reading value.
// 2. Narrow target before reading value.
function onInput(ev: Event): void {
const el = ev.target;
if (el instanceof HTMLInputElement) {
console.log(el.value);
}
}
document.addEventListener("input", onInput);3. React's ChangeEvent types target.
// 3. React's ChangeEvent types target.
function Field() {
function onChange(ev: React.ChangeEvent<HTMLInputElement>): void {
console.log(ev.target.value);
}
return <input onChange={onChange} />;
}Common Gotchas
`event.target` is `EventTarget | null`, not an input
`event.target.value` is a type error. Narrow or use `currentTarget` when that is the element you meant.
A `string` event name disables the map
keep names as literals or as `keyof HTMLElementEventMap`.
React event types are not the DOM ones
mixing them in one handler usually fails assignability.
CustomEvent `detail` is `any` unless you parameterize and augment the map
otherwise every listener re-asserts.