如何在没有类型定义的情况下在 Typescript 中导入节点模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37000981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import node module in Typescript without type definitions?
提问by czerny
When I try to import node.js module in Typescript like this:
当我尝试像这样在 Typescript 中导入 node.js 模块时:
import co = require('co');
import co from 'co';
without providing type definitions, both lines reports same error:
不提供类型定义,两行都报告相同的错误:
error TS2307: Cannot find module 'co'.
How to import it correctly?
如何正确导入?
采纳答案by czerny
The trick is to use purely JavaScript notation:
诀窍是使用纯 JavaScript 符号:
const co = require('co');
回答by Tim Perry
Your options are to either import it outside TypeScript's module system (by calling a module API like RequireJS or Node directly by hand) so that it doesn't try to validate it, or to add a type definition so that you can use the module system and have it validate correctly. You can stub the type definition though, so this can be very low effort.
您的选择是将其导入到 TypeScript 的模块系统之外(通过直接手动调用 RequireJS 或 Node 之类的模块 API)以便它不会尝试验证它,或者添加类型定义以便您可以使用模块系统并让它正确验证。不过,您可以存根类型定义,因此这可以非常省力。
Using Node (CommonJS) imports directly:
直接使用 Node (CommonJS) 导入:
// Note there's no 'import' statement here.
var loadedModule: any = require('module-name');
// Now use your module however you'd like.
Using RequireJS directly:
直接使用 RequireJS:
define(["module-name"], function (loadedModule: any) {
// Use loadedModule however you'd like
});
Be aware that in either of these cases this may mix weirdly with using real normal TypeScript module imports in the same file (you can end up with two layers of module definition, especially on the RequireJS side, as TypeScript tries to manage modules you're also managing by hand). I'd recommend either using just this approach, or using real type definitions.
请注意,在这两种情况中的任何一种情况下,这可能会与在同一文件中使用真正的普通 TypeScript 模块导入奇怪地混合在一起(您最终可能会得到两层模块定义,尤其是在 RequireJS 方面,因为 TypeScript 试图管理您正在使用的模块)也可以手工管理)。我建议要么只使用这种方法,要么使用真正的类型定义。
Stubbing type definitions:
存根类型定义:
Getting proper type definitions would be best, and if those are available or you have time to write them yourself you should definitely should.
获得正确的类型定义将是最好的,如果这些定义可用或者您有时间自己编写它们,您绝对应该这样做。
If not though, you can just give your whole module the any
type, and put your module into the module system without having to actually type it:
如果没有,您可以为整个模块指定any
类型,然后将您的模块放入模块系统中,而无需实际输入:
declare module 'module-name' {
export = <any> {};
}
This should allow you to import module-name
and have TypeScript know what you're talking about. You'll still need to ensure that importing module-name does actually load it successfully at runtime with whatever module system you're using, or it will compile but then fail to actually run.
这应该允许您导入module-name
并让 TypeScript 知道您在说什么。您仍然需要确保导入 module-name 确实在运行时使用您正在使用的任何模块系统成功加载它,否则它将编译但随后无法实际运行。
回答by danvk
I got an error when I used the "Stubbing type definitions" approach in Tim Perry's answer: error TS2497: Module ''module-name'' resolves to a non-module entity and cannot be imported using this construct.
当我在 Tim Perry 的回答中使用“存根类型定义”方法时出现错误: error TS2497: Module ''module-name'' resolves to a non-module entity and cannot be imported using this construct.
The solution was to rework the stub .d.ts
file slightly:
解决方案是.d.ts
稍微修改存根文件:
declare module 'module-name' {
const x: any;
export = x;
}
And then you can import via:
然后您可以通过以下方式导入:
import * as moduleName from 'module-name';
Creating your own stub file lowers the barrier to writing out real declarations as you need them.
创建您自己的存根文件降低了根据需要写出真实声明的障碍。
回答by Oleg I.
Just import the module the following way:
只需按以下方式导入模块:
import 'co';