type StringKeyOf = Extract type CallbackType< T extends Record, EventName extends StringKeyOf, > = T[EventName] extends any[] ? T[EventName] : [T[EventName]] type CallbackFunction< T extends Record, EventName extends StringKeyOf, > = (...props: CallbackType) => any export class EventEmitter> { private callbacks: { [key: string]: Function[] } = {} public on>(event: EventName, fn: CallbackFunction): this { if (!this.callbacks[event]) { this.callbacks[event] = [] } this.callbacks[event].push(fn) return this } protected emit>(event: EventName, ...args: CallbackType): this { const callbacks = this.callbacks[event] if (callbacks) { callbacks.forEach(callback => callback.apply(this, args)) } return this } public off>(event: EventName, fn?: CallbackFunction): this { const callbacks = this.callbacks[event] if (callbacks) { if (fn) { this.callbacks[event] = callbacks.filter(callback => callback !== fn) } else { delete this.callbacks[event] } } return this } protected removeAllListeners(): void { this.callbacks = {} } }