Declaring events in a TypeScript class which extends EventEmitter

Most usable way of doing this, is to use declare:

declare interface MyClass {
    on(event: 'hello', listener: (name: string) => void): this;
    on(event: string, listener: Function): this;
}

class MyClass extends events.EventEmitter {
    emitHello(name: string): void {
        this.emit('hello', name);
    }
}

Note that if you are exporting your class, both the interface and class have to be declared with the export keyword.

Leave a Comment