Extension of eventemitter2, adding typed events.
npm install @bscotch/emittertypescript
import {createEmitter} from '@bscotch/emitter';
type MySimpleEvent = "my-simple-event";
interface MyComplexEvent {
name: "my-complex-event";
// The payload must be a tuple of the arguments that will
// be passed to the event handler.
payload: [arg0: string, arg1: { foo: number }];
}
// For best results, events should be collected in a tuple type
type MyEvents = [MySimpleEvent, MyComplexEvent];
const emitter = createEmitter();
// Now you'll have typed emitter methods!
emitter.on('my-simple-event', () => {});
emitter.emit('my-simple-event');
emitter.on('my-complex-event', (arg0, arg1) => {}); // <- callback is typed!
emitter.emit('my-complex-event', 'foo', { foo: 1 }); // <- arguments are typed!
``