TypeScript for real code · Chapter 30 of 36
A typed event bus
By the end of this chapter you can write down every event once, in an event map, and let a generic class check each event's name and payload, type each handler, and know which events carry nothing. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world TypeScript
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.
Why you need this
The kitchen screen missed twelve orders in one afternoon. Ravi found the cause: the counter app sent 'order:place', the kitchen listened for 'order:placed', and Bunty's event bus passed the typo along without a word. Its names are plain strings and its payloads are any.
Without types, a wrong name is not an error: the event just goes nowhere. A wrong payload is not an error either: the handler crashes, or prints undefined, far away from the line that sent it. You could write one emit function per event, but Tapri has dozens of events.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
EventBus: only known events, each with its payload
Make the class generic over an event map, EventBus<Events>, so new EventBus<TapriEvents>() takes only the event names in TapriEvents, and for each one the payload the map gives it. bus.emit('order:placed', { orderId: 'A1', total: 80 }) and bus.emit('stock:low', { item: 'kulhad', left: 4 }) compile; bus.emit('order:paid', ...) is a type error (not one of Tapri's events), and so are a stock alert with no left and an order whose total is '80' as text. Type the stored handlers too, with no any left, and give the bus at the end of the file its map: new EventBus<TapriEvents>().
emit calls every handler of that event, in the order they were added, and no handler of any other event.
- Task 2
on: the handler gets its event's payload
bus.on(event, handler) listens to one event. The handler's parameter must be exactly that event's payload, with no annotation needed: in bus.on('order:delivered', (delivery) => ...), delivery is TapriEvents['order:delivered'], so reading delivery.total is a type error (a delivery has a rider, but no total), and bus.on('order:paid', ...) is a type error too.
on returns exactly () => void: a function that stops listening, so after off(), the handler is not called again.
- Task 3
once: the same types as on
bus.once(event, handler) is like on, but the handler runs only the next time the event happens: after two stock:low events, a once handler has run one time. It has the same types as on: in bus.once('stock:low', (alert) => ...), alert is exactly TapriEvents['stock:low'], so alert.rider is a type error, and once returns exactly () => void. Calling that function before the event happens means the handler never runs.
- Task 4 · interview question
Interview: emit, no payload for an event that has none
'shop:open' carries nothing (its payload is undefined), so bus.emit('shop:open') must compile with no payload, and bus.emit('shop:open', { at: 9 }) is a type error. Every other event still needs its own payload: bus.emit('order:placed') is a type error. bus.on('shop:open', () => {}) still compiles, and each emit('shop:open') calls its handlers.
Dev will ask how one emit can take a different number of arguments for different events.
What you will learn
- Event maps: one type whose keys are event names and whose values are payloads, read with keyof and indexed access.
- A generic class: the map as the class's type parameter, so one bus works for any set of events, and each handler gets its payload's type.
- Rest parameters with tuple types: a parameter list that changes length, so an event with no payload is sent with no payload.
More in Real-world TypeScript
Build "A typed event bus" yourself
Tapri’s codebase grows, and types keep it honest. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.