javascript - TS1148 ~ 如何使用 --module : "import" and typescript 2. x "none"

标签 javascript typescript browser typescript2.0

我目前正在使用一些旧版 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.jsoninclude 数组中。

关于javascript - TS1148 ~ 如何使用 --module : "import" and typescript 2. x "none",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731431/

相关文章:

javascript - 如何从对象数组中获取键和子键?

typescript - 您正在使用遗留实现。请更新您的代码 : use createWrapper() and wrapper. useWrappedStore()。 nextjs 还原?

reactjs - 类型 'name' 上不存在属性 'EventTarget' - React + TypeScript

authentication - 没有 cookie 的用户/浏览器指纹识别

selenium - 从浏览器使用 Selenium

javascript - 使用 Browserify 跨页面共享公共(public)代码

javascript - 不必要的未捕获、未指定的 "error"事件

javascript - 如何使用 jQuery 检查月份是否等于 12 月

javascript - 合并图像函数导致 iOS 上的混合应用程序结果不正确 - 适用于 Safari

javascript - 如何为静态托管网站设置 Cloud Firestore