xhallix
xhallix

Reputation: 3011

Typescript commonjs inheritance

Was playing around with Ts and got stuck at this

"use strict";

declare const require: any;

const EventEmitter : any = require('events').EventEmitter;

class Foo extends EventEmitter{ //*error* Type 'any' is not a constructor function type.

    constructor() {
        super();
    }
}

I also tried to assign EventEmitter to an interface type of mine, which gave the same error.

How can I extend a class, with commonjs modules using Typescript?

Thank you

Upvotes: 2

Views: 311

Answers (1)

Hoang Hiep
Hoang Hiep

Reputation: 2412

try

"use strict";

import { EventEmitter } from 'events';

class Foo extends EventEmitter {
    constructor() {
        super();
    }
}

You may need to install type definition for 'events' module:

npm install --save @types/node


Update

You can still do the same with require:

"use strict";

import events = require('events');
const EventEmitter = events.EventEmitter;

class Foo extends EventEmitter {
    constructor() {
        super();
    }
}

You should avoid using : any in general so you can use features like IntelliSense and compile-time type checking

Upvotes: 3

Related Questions