2017-08-03 17 views
1

次のTypeScript定義コードでは、Fooインターフェイスのbazプロパティに関数タイプ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 

} 

コピーペースト以外の定義を再利用する方法を見つけることができませんでした。コピー貼りよりも良い方法がありますか?インターフェイスを最初に定義し、メンバーを静的なエクスポートとして再利用する必要がある場合は、問題ありません。 baz

答えて

2

タイプタイプ計算(この場合typeof|)で再利用することができる。

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

しかしtypeinterfaceに何らかの形で異なることに注意してください。例えばclass .. implements ..では使用できません。

+0

ありがとうございます! 'typeof'が答えでした。それは私の特定の質問に答えるのと同じようにすべきです: 'interface Foo = {(a:string):string; baz:typeof baz} 'と入力します。 – Avaq

関連する問題