2017-02-24 3 views
2

은 TS와 함께 주위를 연주하고 있었고, 난이 같은 오류를 준 내 인터페이스 유형에 EventEmitter를 할당하려고이타이프 라이터 commonjs 상속

"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(); 
    } 
} 

에 붙어있어.

Typescript를 사용하여 commonjs 모듈로 클래스를 확장하는 방법은 무엇입니까?

당신은 '이벤트'모듈에 대한 유형 정의를 설치해야 할 수 있습니다 당신이

답변

3

"use strict"; 

import { EventEmitter } from 'events'; 

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

시도 감사 :

npm install --save @types/node


업데이트를

당신은 여전히 ​​require과 같은 작업을 수행 할 수 있습니다 당신은 인텔리 같은 기능을 사용하고

+1

멋진 대답을 유형 검사 시간 컴파일 할 수 있도록

"use strict"; import events = require('events'); const EventEmitter = events.EventEmitter; class Foo extends EventEmitter { constructor() { super(); } } 

당신은 일반적으로 : any을 사용하지 않아야합니다. 이 경우 일반적으로 잘못된 것을 사용하는 이유에 대한 메모를 추가 할 수 있습니까? –

+0

정말 고마워요! 나는 접근법을 정말 좋아하지만, 단지 완성을 위해'require' 솔루션은 여기에 언급 된 것처럼 node.d.ts와 같은 것을 사용하여 가능할 수도있다. http://stackoverflow.com/questions/18378503/importing- node-modules-with-typescript – xhallix

+0

@xhallix 예, 가능합니다. 방금 그 대답을 업데이트했습니다. –