【发布时间】:2016-08-25 20:46:52
【问题描述】:
我有一个使用 .ts 和 .d.ts 文件的 TypeScript 应用程序。使用三斜杠符号/// <reference path=...> 进行引用。我的应用有以下定义文件:
declare module APP {
class Bootstrap {
constructor();
}
}
然后我声明一个名为 "app" 的模块,以便我可以将其导入其他文件:
declare module "app" {
export = APP;
}
假设我的Bootstrap 类存在,然后我使用以下方法导入Bootstrap 类:
import { Bootstrap } from 'app';
这行得通。
现在我在APP 上创建一个子模块,如下所示:
declare module APP.Service {
class Async {
constructor();
}
}
我为子模块做另一个声明:
declare module "app/service" {
export = APP.Service;
}
现在,当我像这样导入 Async 类时:
import { Async } from 'app/service';
我收到以下错误消息:
Module '"app/service"' resolves to a non-module entity and cannot be imported using this construct.``
如何从子模块中导入类?
注意
我通过声明全局 var 找到了解决方法:
declare var APP_SERVICE: APP.Service.IService; // IService exists
并将其导出到我的模块上:
declare module "app/service" {
export = APP_SERVICE;
}
这样做的缺点是我的全局命名空间被我不使用的var 污染了,因为我将使用Service 到App.Service,而不是APP_SERVICE。
【问题讨论】:
标签: javascript typescript ecmascript-6