Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3132

Add type annotations for class that emit events

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • 4.0.0
    • 4.0.0
    • None
    • None
    • Not Needed

    Description

      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 {}
      
      

      Attachments

        Activity

          People

            neal.beeken@mongodb.com Neal Beeken
            neal.beeken@mongodb.com Neal Beeken
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: