2017-08-03 65 views
1

在下面的一段TypeScript定义代码中,我想重复使用baz的函数类型作为Foo接口的baz属性。TypeScript:重新使用接口定义中的函数定义

declare module 'foo' { 

    /* Not all exports will be reused in the interface. */ 
    export function bar(a: string): string 

    /* Imagine a big and verbose function 
    definition with plenty overloadings: */ 
    export function baz(a: number): number 
    export function baz(a: Overloaded): number 

    interface Foo { 

    /* The interface is for a function, so I cannot use module */ 
    (a: string): string 

    /* how do I reuse the full definition of the baz function? */ 
    baz 

    } 

    export default Foo 

} 

我一直没有找到一种方法来重用复制粘贴以外的定义。有没有比复制粘贴更好的方法?如果我必须首先定义接口并将其成员用作静态导出,那就没问题了。

// Foo will contain 2 overloads of baz 
type Foo = { (a: string): string; } | typeof baz; 

然而注意,type是将interface有些不同:

回答

2

baz类型可以与类型的计算(typeof|在这种情况下)进行再利用。例如它不能用于class .. implements ..

+0

谢谢! typeof是答案。它应该是这样回答我的特定问题,虽然:'接口Foo = {(a:字符串):字符串; baz:typeof baz}'。 – Avaq