yet-another-unique-name-ts-event-emitter
v1.1.4
Published
Typed EventEmitter implemented with tsargs
Downloads
14
Maintainers
Readme
ts-event-emitter
Typed EventEmitter implemented with tsargs, based on nodejs EventEmitter.
Install & use
npm i yet-another-unique-name-ts-event-emitter
import { EventEmitter } from 'yet-another-unique-name-ts-event-emitter';
const events = new EventEmitter<{
foo: (a: number, b: string) => void,
}>();
events.emit('foo', 123, 'hello world');
Feature
EventEmitter.emit
's args is fully typed based on events map.
For foo
event in example above, signature is: emit(eventName: string, a: number, b: string)
.
Api
EventEmitter<T>
where T
extends { [eventName]: Call signature }
.
How it works?
Secret is ArgsN from tsargs:
emit<EventKey extends keyof EventMap>(
event: EventKey,
...args: ArgsN<EventMap[EventKey]>
) {
this.emitter.emit(event as string, ...args);
}
PS
Thats absolutely awesome that now on npm there is a lot of trash packages with similar name, but without needed functionality!