相关疑难解决方法(0)

具有TypeScript类的NodeJS EventEmitter

是否可以将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)

javascript node.js typescript typescript1.6

13
推荐指数
3
解决办法
2万
查看次数

在扩展EventEmitter的TypeScript类中声明事件

我有一个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)

node.js typescript

13
推荐指数
5
解决办法
1万
查看次数

标签 统计

node.js ×2

typescript ×2

javascript ×1

typescript1.6 ×1