I have extended a class that inherits from the EventEmmiter
class.
For better support and auto-completion in IntelliSense, I wanted to display the available events to listen to. Here is my solution:
//Imports from index.ts
import {
CommandContext,
CommandErrorContext,
CommandHandler
} from '../../';
import { EventEmitter } from 'events';
export class CommandHandler extends CommandHandlerEvents {
constructor() {
super();
}
}
export declare class CommandHandlerEvents extends EventEmitter {
on(event: string, listener: Function): this;
on(event: 'failure', listener: (handler: CommandHandler, context: CommandErrorContext) => void): this;
on(event: 'success', listener: (handler: CommandHandler, context: CommandContext) => void): this;
once(event: string, listener: Function): this;
once(event: 'failure', listener: (handler: CommandHandler, context: CommandErrorContext) => void): this;
once(event: 'success', listener: (handler: CommandHandler, context: CommandContext) => void): this;
emit(event: string, args: any[]): boolean;
emit(event: 'failure', args: [CommandHandler, CommandErrorContext]): boolean;
emit(event: 'success', args: [CommandHandler, CommandContext]): boolean;
}
The code above meets my requirements perfectly, but when I try to create an instance of the CommandHandler
class, it throws an error:
Uncaught ReferenceError: CommandHandlerEvents is not defined
I've come across suggestions to use interfaces for the same purpose, however, switching CommandHandlerEvents
from a declare class
to an interface
causes IntelliSense suggestions to disappear.
Here is an example of the desired outcome: image