← Back to TypeScript Course | Chapter 12: TypeScript with DOM | Lesson 3 of 6

Event Handling in TypeScript

TypeScript provides types for browser events such as MouseEvent and KeyboardEvent. Typed event handlers make event properties easier and safer to use.
Syntax
typescript
element.addEventListener("eventName", (event: EventType) => {
  // handle event
});

Click Events

A click handler's event parameter is typed as MouseEvent, which gives you access to coordinates and modifier-key state (like event.shiftKey) with full autocomplete instead of an untyped generic Event.

Example: Click Events

typescript
const button = document.createElement("button");
button.addEventListener("click", (event: MouseEvent) => {
  console.log("Clicked at", event.clientX, event.clientY);
});
button.dispatchEvent(new MouseEvent("click", { clientX: 10, clientY: 20 }));

Keyboard Events

Keyboard event handlers receive a KeyboardEvent, whose .key and .code properties let you distinguish exactly which key was pressed, which MouseEvent and other event types simply don't expose.

Example: Keyboard Events

typescript
document.addEventListener("keydown", (event: KeyboardEvent) => {
  console.log("Key pressed:", event.key);
});
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter" }));

Input Events

Input events on form fields are typed as Event with the target narrowed to something like HTMLInputElement, since the base Event type alone doesn't know it's attached to an input with a .value property.

Example: Input Events

typescript
const input = document.createElement("input");
input.addEventListener("input", (event: Event) => {
  const target = event.target as HTMLInputElement;
  console.log("Value is now:", target.value);
});
input.value = "hi";
input.dispatchEvent(new Event("input"));

Event Targets

event.target is typed generically as EventTarget | null, so reading a DOM-specific property off it requires narrowing or casting to the concrete element type you expect the event to have actually come from.

Example: Event Targets

typescript
document.addEventListener("click", (event: Event) => {
  const target = event.target as HTMLElement | null;
  console.log("Clicked element:", target?.tagName);
});
document.dispatchEvent(new MouseEvent("click"));

Removing Event Listeners

removeEventListener requires passing the exact same function reference that was registered with addEventListener, so an inline anonymous arrow function can never be removed later — store the handler in a named variable if you'll need to detach it.

Example: Removing Event Listeners

typescript
function handleClick() {
  console.log("Handled");
}
const button = document.createElement("button");
button.addEventListener("click", handleClick);
button.removeEventListener("click", handleClick);
console.log("Listener removed using the same named function reference");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Typing an input handler as MouseEvent, or reading event.key from a click event, where those properties do not exist.
  2. Using event.target.value directly, when target is EventTarget | null and needs narrowing.
  3. Typing the callback with the wrong event name string, so the event type does not match, like "click" with a KeyboardEvent.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.