Typed Event Emitter
A ~20-line pub/sub where the event names and each event's payload type come from one map, so `on` and `emit` are fully checked against it and `on` returns its own unsubscribe.
3 min read
#frontendA string-keyed event bus is quick to write and just as quick to get
wrong - emit('user:login', {}) with the wrong shape fails silently.
Anchor the whole thing to one Record<eventName, payload> map instead,
and on / emit type-check against it, with the payload narrowed per
event.
@itsnas usage.ts
codeusage.ts
main
Nas (@itsnas)
main
Nas (@itsnas)