2

.d.tsファイルを作成する正しい方法が何であるかを理解するのに役立つ必要があります。私を投げた何TypeScript .d.tsの構文 - エクスポートと宣言

は、一部の人々は、この構文を使用することです:

// lib-a.d.ts 
namespace My.Foo.Bar { 
    interface IFoo {} 
    interface IBar {} 
} 

// lib-b.d.ts 
declare namespace My.Foo.Bar { 
    interface IFoo {} 
    interface IBar {} 
} 

// lib-c.d.ts 
namespace My.Foo.Bar { 
    export interface IFoo {} 
    export interface IBar {} 
} 

// lib-d.d.ts 
declare namespace My.Foo.Bar { 
    export interface IFoo {} 
    export interface IBar {} 
} 

// lib-e.d.ts 
declare module My.Foo.Bar { 
    export interface IFoo {} 
    export interface IBar {} 
} 

どちらが正しいですか?宣言は何のために使われていますか?輸出は何に使用されていますか?名前空間対モジュールを使用するのはいつですか?

答えて

1

正しい方法は次のとおりです。

declare namespace NS { 
    interface InterfaceTest { 
     myProp: string; 
    } 

    class Test implements InterfaceTest { 
     myProp: string; 
     myFunction(): string; 
    } 
} 

あなたは常にいくつかの.tsファイルを書き込み、--declarationオプション(tsc test.ts --declaration)でそれをコンパイルして、正しい署名を確認することができます。これにより、適切な入力を伴うd.tsファイルが生成されます。

namespace NS { 
    export interface InterfaceTest { 
     myProp: string; 
    } 

    export class Test implements InterfaceTest { 
     public myProp: string = 'yay'; 

     public myFunction() { 
      return this.myProp; 
     } 
    } 

    class PrivateTest implements InterfaceTest { 
     public myPrivateProp: string = 'yay'; 

     public myPrivateFunction() { 
      return this.myProp; 
     } 
    } 
} 

は、例えば上記の宣言ファイルには、次のコードから生成されました

関連する問題