我目前正在使用一些旧版 JavaScript 开发一个项目。该应用程序不包含模块加载器,它只是将所有内容作为全局变量放入 window 对象中。遗憾的是,接触遗留代码并包含模块加载器对我来说不是一个可行的选择。
我想在我自己的代码中使用 typescript 。我设置了 typescript 编译器选项
module : "none"
在我的 tsconfig.json 中,我只使用命名空间来组织我自己的代码。到目前为止效果很好。
.. 到现在为止:
import * as Rx from 'rxjs';
..
Rx.Observable.from(['foo',bar']);
...
// Results in TypeScript-Error:
// TS1148:Cannot use imports, exports, or module augmentations when '--module' is 'none'.
设置了“module-none”选项后,您不能在 typescript 中使用导入语句。
如何将外部库包含在此 typescript 设置中?
有可能吗?
到目前为止我尝试了什么(包括来自 RxJs-Library 的 Rx)
///<reference path="../node_modules/rxjs/Rx.d.ts" />
..
Rx.Observable.from(['foo',bar']);
...
// Results in TypeScript-Error -> TS2304:Cannot find name 'Rx'.
在此Related Question , Kirill Dmitrenko 建议使用引用标签,但对我不起作用。
我最终得到了这个结构。
declare const Rx: any;
有效,但是你失去了类型检查和智能感知:(
最佳答案
为 Rx
创建一个全局声明,它是 RxJS 导出的类型,如下所示:
import * as RxJS from "rxjs";
declare global {
const Rx: typeof RxJS;
}
export {};
将其保存到文件(例如 global.d.ts
),然后将其添加到 tsconfig.json
的 include
数组中。
关于javascript - TS1148 ~ 如何使用 --module : "import" and typescript 2. x "none",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731431/