2013-05-27 6 views
5

클래스와 같은 타입 스크립트 선언 파일 안의 인터페이스 나 변수를 클래스에서 확장 할 수있는 방법이 있습니까? 이처럼Typescript의 선언 인터페이스에서 클래스 확장하기

:

declare module "tedious" { 

    import events = module('events'); 

    export class Request extends event.EventEmitter { 
     constructor (sql: string, callback: Function); 
     addParameter(name: string, type: any, value: string):any; 
     addOutputParameter(name: string, type: any): any; 
     sql:string; 
     callback: Function; 
    }; 

} 

는 지금은이 같은 EventEmitter 인터페이스를 재정의하고 내 자신의 EventEmitter 선언을 사용해야합니다.
import events = module('events'); 

class EventEmitter implements events.NodeEventEmitter{ 
    addListener(event: string, listener: Function); 
    on(event: string, listener: Function): any; 
    once(event: string, listener: Function): void; 
    removeListener(event: string, listener: Function): void; 
    removeAllListener(event: string): void; 
    setMaxListeners(n: number): void; 
    listeners(event: string): { Function; }[]; 
    emit(event: string, arg1?: any, arg2?: any): void; 
} 

export class Request extends EventEmitter { 
    constructor (sql: string, callback: Function); 
    addParameter(name: string, type: any, value: string):any; 
    addOutputParameter(name: string, type: any): any; 
    sql:string; 
    callback: Function; 
}; 

그리고 내 타이프 라이터 파일

import tedious = module('tedious'); 

class Request extends tedious.Request { 
    private _myVar:string; 
    constructor(sql: string, callback: Function){ 
     super(sql, callback); 
    } 
} 

답변

1

그것은 좋은 예를 작동합니다 내부 나중에 확장 : 당신이 .D에 넣을 수 있습니다 파일에 넣어

// Code in a abc.d.ts 
declare module "tedious" { 
    export class Request { 
     constructor (sql: string, callback: Function); 
     addParameter(name: string, type: any, value: string):any; 
     addOutputParameter(name: string, type: any): any; 
     sql:string; 
     callback: Function; 
    }; 
} 

// Your code: 
///<reference path='abc.d.ts'/> 
import tedious = module('tedious'); 

class Request extends tedious.Request { 
    private _myVar:string; 
    constructor(sql: string, callback: Function){ 
     super(sql, callback); 
    } 
} 

아무것도. TS 파일.

Try it

+1

하지만 그때 나는 모든 EventEmitter 구현을 놓치고 Typescript는 누락 된 속성 오류를 발생시킬 것입니다. 이것이 내가 처음으로 NodeJS EventEmmiter 클래스를 재정의 한 이유입니다. – Manuel

2

나는 약 다시 2013 년 몰라,하지만 지금은 쉽게 충분하다 : 나는이에 대한 답을 찾고 있었다

/// <reference path="../typings/node/node.d.ts" /> 
import * as events from "events"; 

class foo extends events.EventEmitter { 
    constructor() { 
     super(); 
    } 

    someFunc() { 
     this.emit('doorbell'); 
    } 
} 

, 그리고 마지막으로 그것을 알아 냈다.