2017-08-03 111 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