Model the event catalog as a map from event names to payloads, then derive both the event name and payload types from that map. A discriminant or distinct key prevents mismatched payloads: ```ts type EventMap = { userCreated: { readonly userId: string }; invoicePaid: { readonly invoiceId: string; readonly amountCents: number }; }; type EventName = keyof EventMap; type Listener<K extends EventName> = (payload: EventMap[K]) => void; export class EventBus { private readonly listeners: { [K in Ev...
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill typescript-language --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Typescript Language?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-typescript-language-5daba104)More formats (shields.io, HTML) on the badges page.
Model the event catalog as a map from event names to payloads, then derive both the event name and payload types from that map. A discriminant or distinct key prevents mismatched payloads:
```ts
type EventMap = {
userCreated: { readonly userId: string };
invoicePaid: { readonly invoiceId: string; readonly amountCents: number };
};
type EventName = keyof EventMap;
type Listener<K extends EventName> = (payload: EventMap[K]) => void;
export class EventBus {
private readonly listeners: {
[K in EventName]?: Set<Listener<K>>;
} = {};
public on<K extends EventName>(
event: K,
listener: Listener<K>,
): () => void {
const listeners = this.listeners[event] ?? new Set<Listener<K>>();
this.listeners[event] = listeners;
listeners.add(listener);
return () => listeners.delete(listener);
}
public emit<K extends EventName>(event: K, payload: EventMap[K]): void {
this.listeners[event]?.forEach((listener) => listener(payload));
}
}
const bus = new EventBus();
bus.on("invoicePaid", ({ amountCents }) => console.log(amountCents));
bus.emit("invoicePaid", { invoiceId: "inv-1", amountCents: 2500 });
```
`emit("invoicePaid", { userId: "u-1" })` is rejected at compile time. Keep the catalog and callbacks free of `any`; use explicit parameters/returns and readonly payloads.
Is this your skill, or is something wrong with this listing? . Author removals are honored within 72 hours.
No comments yet. Be the first to comment!