次のように、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
と、ラッパーが削除され、エラーが解決されます。
それは期待される動作ですか?同じファイル内でモジュールの外部でインターフェイスを定義すると、特にそのインターフェイスがモジュール内で使用されていない場合、モジュールが突然「閉じられる」理由がわかりません。