1

次のように、2つのファイルにまたがる2つのモジュールがあります。

--App.Tools.Utils.ts

export interface ILogger {
    log: (msg: string) => void;
}

module App.Tools.Utils {     


    export class Logger implements ILogger { ... }

    export function someFunction(){ ... }
}

次に、2 番目のファイルでモジュールをインポートします。

--App.Routers.ts

 /// <reference path="App.Tools.Utils.ts" />
 module App.Routers { 

    import Utils = App.Tools.Utils;  //ERROR: A module cannot be aliased to a non-module type        

}

最終的に、解決策は ILogger インターフェイスを App.Tools.Utils モジュール内に移動してエラーを解決することであることがわかりました。最初はある程度理解できました。モジュールに含まれていないインターフェイスを Logger クラスが実装しているため、コンパイラはモジュールをインポートできないと考えました。テストするために、ILogger インターフェイスをモジュール内に移動しました (エラーは解決されました) が、モジュールのすぐ外側に任意のインターフェイス (モジュール内またはどこでも使用されていないもの) を追加すると、エラーが返されます..

export interface INeverUsed { } //generates same error

module App.Tools.Utils {     

    export interface ILogger {
       log: (msg: string) => void;
    }

    export class Logger implements ILogger { ... }

    export function someFunction(){ ... }
}

生成された JS を見ると、モジュールの外側にインターフェイスを追加するとdefine(["require", "exports"]ラッパーが生成され、他のファイルに App.Tools.Utils モジュールをインポートしようとするとエラーが発生します。インターフェイスを削除するdefineと、ラッパーが削除され、エラーが解決されます。

それは期待される動作ですか?同じファイル内でモジュールの外部でインターフェイスを定義すると、特にそのインターフェイスがモジュール内で使用されていない場合、モジュールが突然「閉じられる」理由がわかりません。

4

1 に答える 1

2

Because you are using the export keyword on the interface, the compiler is treating the file as a module.

If you remove the export keyword on the interface, it should work:

interface ILogger {
    log: (msg: string) => void;
}

module App.Tools.Utils {     
    export class Logger implements ILogger { 
        log(msg: string) { }
    }

    export function someFunction(){  }
}

And

///<reference path="game.ts" />

module App.Routers { 
    import Utils = App.Tools.Utils; // works      
}
于 2013-01-14T11:39:37.863 に答える