typescript
45 lines · 8 steps
A type-safe event emitter in TypeScript
How generics tie event names to their exact argument types so listeners and emits stay in sync at compile time.
Explained by
highlit
1type EventMap = Record<string, unknown[]>;
2
3type Listener<Args extends unknown[]> = (...args: Args) => void;
4
5export class TypedEmitter<Events extends EventMap> {
6 private listeners: {
7 [K in keyof Events]?: Set<Listener<Events[K]>>;
8 } = {};
9
10 on<K extends keyof Events>(event: K, listener: Listener<Events[K]>): this {
11 (this.listeners[event] ??= new Set()).add(listener);
12 return this;
13 }
14
15 once<K extends keyof Events>(event: K, listener: Listener<Events[K]>): this {
16 const wrapper: Listener<Events[K]> = (...args) => {
17 this.off(event, wrapper);
18 listener(...args);
19 };
20 return this.on(event, wrapper);
21 }
22
23 off<K extends keyof Events>(event: K, listener: Listener<Events[K]>): this {
24 this.listeners[event]?.delete(listener);
25 return this;
26 }
27
28 emit<K extends keyof Events>(event: K, ...args: Events[K]): boolean {
29 const handlers = this.listeners[event];
30 if (!handlers || handlers.size === 0) return false;
31 for (const handler of [...handlers]) {
32 handler(...args);
33 }
34 return true;
35 }
36
37 removeAllListeners<K extends keyof Events>(event?: K): this {
38 if (event === undefined) {
39 this.listeners = {};
40 } else {
41 delete this.listeners[event];
42 }
43 return this;
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Mapping an event-name type to its argument tuple lets the compiler check every listener and emit for you.
- 2Copying the handler set before iterating avoids skipping or double-firing when handlers mutate the set mid-emit.
- 3A `once` listener is just a normal listener that removes itself before delegating to the real callback.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express'; import { DataSource } from 'typeorm';
Per-tenant database connections in NestJS
multi-tenancy
connection-pooling
dependency-injection
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-type-safe-event-emitter-in-typescript-explained-typescript-3318/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.