Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  25] [ 6]  / answers: 1 / hits: 30897  / 9 Years ago, sun, november 15, 2015, 12:00:00

Is it possible to use NodeJS' events.EventEmitter with a TypeScript class? If yes, how?



I've tried countless variations in the last hours to get this working, so I won't list any of them.



What I basically want to do:



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');
});
}
}

More From » node.js

 Answers
11

You should download node typings:



$ tsd install node --save


and then just use the following code:



///<reference path=./typings/node/node.d.ts />
import events = require('events');

class Database{
constructor() {
events.EventEmitter.call(this);
}
}


I simplified it to test your main problem.



Edit: Modified based on your comment:



///<reference path=./typings/node/node.d.ts />
import events = require('events');

class Database extends events.EventEmitter {
constructor() {
super();
this.emit('ready');
}
}

new Database();

[#64395] Thursday, November 12, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daijab

Total Points: 60
Total Questions: 99
Total Answers: 110

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
;