Skip to content
dreamcode
dreamcode
Projects
Event Router
Independent projectTypeScript
Reward: +340 XP
PROJECT BRIEF

Event Router

An app emits three kinds of event, modelled as a discriminated union on kind. Write describeEvents(events) that turns each event into one line of text:

  • click: click at (X, Y)
  • key: key K, or shortcut Ctrl+K when ctrl is true
  • scroll: scroll down N for a positive delta, scroll up N for a negative one (N is always positive)

Handle every kind in a switch, and finish with a never check so adding a fourth kind later becomes a compile error instead of a silent bug.

Examples
describeEvents([{ kind: "click", x: 10, y: 20 }, { kind: "key", key: "s", ctrl: true }, { kind: "scroll", delta: -40 }])
["click at (10, 20)", "shortcut Ctrl+s", "scroll up 40"]
describeEvents([{ kind: "key", key: "Enter", ctrl: false }, { kind: "scroll", delta: 15 }])
["key Enter", "scroll down 15"]
project.ts
TYPESCRIPT
Saved as you type

Tests

0 of 3 passing
  • one of each
    describeEvents([{ kind: "click", x: 10, y: 20 }, { kind: "key", key: "s", ctrl: true }, { kind: "scroll", delta: -40 }])
    expected ["click at (10, 20)", "shortcut Ctrl+s", "scroll up 40"]
  • plain key and scroll down
    describeEvents([{ kind: "key", key: "Enter", ctrl: false }, { kind: "scroll", delta: 15 }])
    expected ["key Enter", "scroll down 15"]
  • no events
    describeEvents([])
    expected []
On the line
+340 XP
Pass all 3 tests to claim it.