我有一个类 extends EventEmitter
可以发出事件 hello
。如何声明具有特定事件名称和侦听器签名的 on
方法?
class MyClass extends events.EventEmitter {
emitHello(name: string): void {
this.emit('hello', name);
}
// compile error on below line
on(event: 'hello', listener: (name: string) => void): this;
}
原文由 aleung 发布,翻译遵循 CC BY-SA 4.0 许可协议
最有用的方法是使用
declare
:请注意,如果要 导出 类, 则 必须使用
export
关键字声明接口和类。