-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 4.0.0
-
Component/s: None
We should type annotate our EventEmitters, here's some sample code of how to go about it:
import { EventEmitter } from 'events'; type GenericListener = (...args: any[]) => void; /** * Event description type * @public */ export type EventsDescription = Record<string, GenericListener>; type CommonEvents = 'newListener' | 'removeListener'; /** * Typescript type safe event emitter * @public */ export declare interface TypedEventEmitter<Events extends EventsDescription> extends EventEmitter { addListener<EventKey extends keyof Events>(event: EventKey, listener: Events[EventKey]): this; addListener( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; addListener(event: symbol, listener: GenericListener): this; on<EventKey extends keyof Events>(event: EventKey, listener: Events[EventKey]): this; on( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; on(event: symbol, listener: GenericListener): this; once<EventKey extends keyof Events>(event: EventKey, listener: Events[EventKey]): this; once( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; once(event: symbol, listener: GenericListener): this; removeListener<EventKey extends keyof Events>(event: EventKey, listener: Events[EventKey]): this; removeListener( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; removeListener(event: symbol, listener: GenericListener): this; off<EventKey extends keyof Events>(event: EventKey, listener: Events[EventKey]): this; off( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; off(event: symbol, listener: GenericListener): this; removeAllListeners<EventKey extends keyof Events>(event?: EventKey | CommonEvents | symbol): this; listeners<EventKey extends keyof Events>( event: EventKey | CommonEvents | symbol ): Events[EventKey][]; rawListeners<EventKey extends keyof Events>( event: EventKey | CommonEvents | symbol ): Events[EventKey][]; emit<EventKey extends keyof Events>( event: EventKey | symbol, ...args: Parameters<Events[EventKey]> ): boolean; listenerCount<EventKey extends keyof Events>(type: EventKey | CommonEvents | symbol): number; prependListener<EventKey extends keyof Events>(event: EventKey, listener: Events[EventKey]): this; prependListener( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; prependListener(event: symbol, listener: GenericListener): this; prependOnceListener<EventKey extends keyof Events>( event: EventKey, listener: Events[EventKey] ): this; prependOnceListener( event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void ): this; prependOnceListener(event: symbol, listener: GenericListener): this; eventNames(): string[]; getMaxListeners(): number; setMaxListeners(n: number): this; } /** * Typescript type safe event emitter * @public */ export class TypedEventEmitter<Events extends EventsDescription> extends EventEmitter {}