是否可以将NodeJS events.EventEmitter与TypeScript类一起使用?如果有,怎么样?
我在最后几个小时尝试了无数的变化来使这个工作,所以我不会列出任何一个.
我基本上想做什么:
export class Database{
constructor(cfg:IDatabaseConfiguration) {
// events.EventEmitter.call(this);
mongoose.connect(cfg.getConnectionString(), cfg.getCredentials(), function (err:any) {
if (err)
this.emit('error', err);
else
this.emit('ready');
});
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个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;
}
Run Code Online (Sandbox Code Playgroud)