Skip to content

Proposal/Discussion: NodeJS.EventEmitter should be generic #25302

@SimonSchick

Description

@SimonSchick

If you know how to fix the issue, make a pull request instead.

If you do not mention the authors the issue will be ignored.

I was wondering if at some point the definition of EventEmitter should be changed to be more generic eg.

    export class EventEmitter<
    T extends { [key: string]: any } = { [key: string]: any },
    > {
        static listenerCount(emitter: EventEmitter, event: string | symbol): number; // deprecated
        static defaultMaxListeners: number;

        addListener<K extends keyof T>(event: K, listener: (arg: T[K]) => void): this;
        ...

This would drastically improve type safety for consumers and simplify definitions.

Known issues:

  • 2nd degree inheritance cannot easily
  • symbol is not supported as a generic index type.
  • multiple arguments are not possible to be implemented this way eg. ...args

All of these needs to be resolved either by manually declaring the signatures (as done right now) or via declaration merging (also done by some right now).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions