Lesson 25 — Ultimate: typed event emitter
Boss level: a fully typed event bus
An event emitter (or bus) lets parts of an app talk without knowing each other: some code calls on(name, handler) to listen, other code calls emit(name, data). An event map type lists every event name with its data type, and generics with K extends keyof E make sure emit("login", …) gets exactly the login data and each handler receives the right type.
type Events = { saved: { id: number } };
bus.on("saved", (d) => d.id); // d is { id: number }
// bus.emit("saved", { id: "x" }); ← error
Why it matters: this is production-grade type safety for the event systems in UI libraries and Node.
Finish emit so it calls every handler registered for that event. Expect welcome Sam, then bye.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based TypeScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for TypeScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →