Skip to content

EventWithTarget<T extends HTMLElement = HTMLElement, E extends Event = Event> ​

ts
type EventWithTarget<T extends HTMLElement = HTMLElement, E extends Event = Event> = E & object;

Allows setting the target property of a generic DOM event to a specific type of HTMLElement. Most of the time the typed EventTarget hinders to properly access the targetted element.

If the generics are not passed, the defaults are HTMLElement and Event.

Type Parameters ​

T ​

T extends HTMLElement = HTMLElement

E ​

E extends Event = Event

Type Declaration ​

target ​

ts
target: T;

Example ​

ts
button.addEventListener('click', (event: EventWithTarget<HTMLButtonElement>) => {
  event.target.disabled = true;
});