type StringKeyOf = Extract type CallbackType, EventName extends StringKeyOf> = T[EventName] extends any[] ? T[EventName] : [T[EventName]] type CallbackFunction, EventName extends StringKeyOf> = ( ...props: CallbackType ) => any export class EventEmitter> { private callbacks: { [key: string]: Array<(...args: any[]) => void> } = {} public on>(event: EventName, fn: CallbackFunction): this { if (!this.callbacks[event]) { this.callbacks[event] = [] } this.callbacks[event].push(fn) return this } public 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 } public once>(event: EventName, fn: CallbackFunction): this { const onceFn = (...args: CallbackType) => { this.off(event, onceFn) fn.apply(this, args) } return this.on(event, onceFn) } public removeAllListeners(): void { this.callbacks = {} } }