2013-05-27 46 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'); 
    } 
} 

我一直在寻找这个问题的答案,终于想通了。

相关问题