TS1148〜如何“导入”商品使用--module:“ none”和打字稿2.x [英] TS1148 ~ How to "import" with --module: "none" and typescript 2.x
问题描述
我目前正在使用一些旧版JavaScript进行项目。该应用程序不包括模块加载器,它只是将所有内容作为全局对象放入窗口对象中。遗憾的是,接触遗留代码并包括模块加载器对我来说不是一个可行的选择。
I'm currently working on a project with some legacy javascript. The application does not include a module loader, it just puts everything as a global into the window object. Touching the legacy code and including a module loader is sadly not a viable option for me.
我想为自己的代码使用打字稿。我设置了打字稿编译器选项
I want to use typescript for my own code. I set the typescript compiler-option
module : "none"
,我仅使用名称空间来组织自己的代码。
in my tsconfig.json and I only used namespaces to organize my own code. Worked well so far.
..直到现在:
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'.
使用无模块选项集,则不能在打字稿中使用import语句。
如何在此打字稿设置中包含外部库?
甚至可能吗?
With the "module-none" option set, you can't use import statements in your typescript.
How can you include external libs with this typescript setup?
Is it even possible?
我到目前为止所做的尝试(包括RxJs-Library中的Rx)
What I tried so far (to include Rx from the RxJs-Library)
///<reference path="../node_modules/rxjs/Rx.d.ts" />
..
Rx.Observable.from(['foo',bar']);
...
// Results in TypeScript-Error -> TS2304:Cannot find name 'Rx'.
在此,基里尔·德米特连科(Kirill Dmitrenko)引用了参考标签,对我不起作用。
In this Related Question, Kirill Dmitrenko sugested using an reference-tag, didn´t work for me.
我最终得到了这个构造。
I ended up with this construct.
declare const Rx: any;
可以,但是您松开了类型检查和智能感知功能:(
Works, but you loose type-checks and intellisense :(
推荐答案
为 Rx
创建一个全局声明,该声明是RxJS导出的类型,例如:
Create a global declaration for Rx
which is the type of the RxJS exports, like so:
import * as RxJS from "rxjs";
declare global {
const Rx: typeof RxJS;
}
export {};
将其保存到文件中(例如 global.d.ts
),然后将其添加到 tsconfig.json的
。 include
数组中
Save it to a file (e.g. global.d.ts
) and then add that to the include
array of tsconfig.json
.
这篇关于TS1148〜如何“导入”商品使用--module:“ none”和打字稿2.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!