TypeScript: Event handlers named by template literal types

Name event handlers with a mapped type and template literal keys, so each handler is checked against its own event's data.

What to build

A program that dispatches input events to typed handlers.

Requirements

  • Declare interface Events { click: { x: number; y: number }; key: { key: string }; resize: { width: number; height: number } } and a Handlers type mapped from it with keys onClick, onKey and onResize.
  • Handlers: onClick returns clicked at (X, Y), onKey returns pressed KEY, onResize returns resized to WxH.
  • Input lines are click X Y, key NAME or resize W H; print each handler's result. Any other event name prints no handler for NAME.
  • At the end print click: A, key: B, resize: C, how many of each were handled.

Input

Line 1: n. Then n lines.

Output

n lines, then the counts.

Constraints

1 ≤ n ≤ 50.

Example

Input:

4
click 3 4
key Enter
scroll 10
resize 800 600

Output:

clicked at (3, 4)
pressed Enter
no handler for scroll
resized to 800x600
click: 1, key: 1, resize: 1

Why it works

The handler names and their parameter types are both derived from Events, so onResize must accept exactly a width and a height, and adding an event to the interface makes the handlers object incomplete until it is handled.

Examples

Example 1 · Input
4
click 3 4
key Enter
scroll 10
resize 800 600
Expected output
clicked at (3, 4)
pressed Enter
no handler for scroll
resized to 800x600
click: 1, key: 1, resize: 1
Example 2 · Input
2
click 0 0
click -1 5
Expected output
clicked at (0, 0)
clicked at (-1, 5)
click: 2, key: 0, resize: 0

Hints

Hint 1

type Handlers = { [E in keyof Events as `on${Capitalize<E>}`]: (event: Events[E]) => string }.

Hint 2

Parse each line into an event name and its data, then look up "on" + the capitalised name.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

interface Events {
  click: { x: number; y: number };
  key: { key: string };
  resize: { width: number; height: number };
}

type Handlers = { [E in keyof Events as `on${Capitalize<E>}`]: (event: Events[E]) => string };

const handlers: Handlers = {
  onClick: (e) => "clicked at (" + e.x + ", " + e.y + ")",
  onKey: (e) => "pressed " + e.key,
  onResize: (e) => "resized to " + e.width + "x" + e.height,
};

const counts: Record<keyof Events, number> = { click: 0, key: 0, resize: 0 };
const n = Number(read());
for (let i = 0; i < n; i++) {
  const [name, a, b] = read().split(" ");
  if (name === "click") {
    console.log(handlers.onClick({ x: Number(a), y: Number(b) }));
    counts.click++;
  } else if (name === "key") {
    console.log(handlers.onKey({ key: a }));
    counts.key++;
  } else if (name === "resize") {
    console.log(handlers.onResize({ width: Number(a), height: Number(b) }));
    counts.resize++;
  } else {
    console.log("no handler for " + name);
  }
}
console.log("click: " + counts.click + ", key: " + counts.key + ", resize: " + counts.resize);

Lesson: Mapped, conditional and template literal types →