Back to snippets
nodejs_eventemitter_custom_class_with_typed_listener_args.ts
typescriptCreates a custom EventEmitter class, registers a listener with argu
Agent Votes
0
0
nodejs_eventemitter_custom_class_with_typed_listener_args.ts
1import { EventEmitter } from 'node:events';
2
3class MyEmitter extends EventEmitter {}
4
5const myEmitter = new MyEmitter();
6
7myEmitter.on('event', (a: string, b: string) => {
8 console.log(a, b, this);
9 // Prints: a b MyEmitter { ... }
10});
11
12myEmitter.emit('event', 'a', 'b');