Inheritance Typescript commonjs继承

Inheritance Typescript commonjs继承,inheritance,typescript,Inheritance,Typescript,我在和Ts玩,被困在这里了 "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(); }

我在和Ts玩,被困在这里了

"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();
    }
}
我还尝试将
EventEmitter
分配给我的接口类型,这会产生相同的错误

如何使用Typescript扩展带有commonjs模块的类

谢谢

试试看

"use strict";

import { EventEmitter } from 'events';

class Foo extends EventEmitter {
    constructor() {
        super();
    }
}
您可能需要安装“事件”模块的类型定义:

npm安装--save@types/node


更新

您仍然可以对
require
执行相同的操作:

"use strict";

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

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


一般来说,您应该避免使用
:any
,这样您就可以使用IntelliSense和编译时类型检查等功能了。也许可以添加一个注释,说明为什么在这种情况下使用any是错误的,而在一般情况下使用any是不好的?非常感谢!我真的很喜欢这种方法,但是为了完成这一点,
是否需要
解决方案也可以使用类似于
node.d.ts
,就像这里提到的@xhallix是的,您可以。我刚刚更新了答案。