编写自定义 TypeScript 定义文件时出现错误“模块‘名称’解析为无类型模块...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42388217/
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
Having error "Module 'name' resolves to an untyped module at..." when writing custom TypeScript definition file
提问by hirikarate
I can't find TypeScript definition @type/{name}
for one of my installed NodeJS packages, so I attempt to write a d.ts
file for it, and put the file in {project root}\typings
folder. This is how I do:
我找不到@type/{name}
我安装的 NodeJS 包之一的TypeScript 定义,所以我尝试d.ts
为它编写一个文件,并将文件放在{project root}\typings
文件夹中。这就是我的做法:
// My source code: index.ts
import Helper from 'node-helper-lib';
// My definition: \typings\node-helper-lib.d.ts
declare....(something else)
declare module 'node-helper-lib' {
class Helper { ... }
export = Helper;
}
However, Visual Studio Code keeps yielding this error and puts red line under declare module 'node-helper-lib'
:
但是,Visual Studio Code 不断产生此错误并在declare module 'node-helper-lib'
以下位置放置红线:
[ts] Invalid module name in augmentation. Module 'node-helper-lib' resolves to an untyped module at '{project path}\node_modules\node-helper-lib\index.js', which cannot be augmented.
[ts] 扩充中的模块名称无效。模块 'node-helper-lib' 解析为 '{project path}\node_modules\node-helper-lib\index.js' 处的无类型模块,该模块无法扩充。
Isn't it legit that because the library is untyped, so I should be allowed to add typing to it?
因为库是无类型的,所以我应该被允许向它添加类型,这不是合法的吗?
UPDATE:
更新:
I am using:
我在用:
- TypeScript: 2.1.4
- Visual Studio Code: 1.9.1
- Node JS: 6.9.4
- Windows 10 x64
- 打字稿:2.1.4
- Visual Studio 代码:1.9.1
- 节点JS:6.9.4
- 视窗 10 x64
采纳答案by hirikarate
After some tries and errors, I found that augmentation
means "declaring a module in the same file with other module declaration(s)".
经过一些尝试和错误,我发现这augmentation
意味着“在同一个文件中声明一个模块与其他模块声明”。
Therefore if we want to write a definition file for an untyped3rd-party JavaScript library, we must have ONLY ONE declare module 'lib-name'
in that file, and 'lib-name' must exactly match the library name (can be found in its package.json, "name" property).
因此,如果我们要为无类型的3rd-party JavaScript 库编写定义文件,则该文件中必须只有一个declare module 'lib-name'
,并且 'lib-name' 必须与库名称完全匹配(可以在其 package.json 中找到)名称”属性)。
On the other hand, if a 3rd-party library already has definition file.d.ts
included, and we want to extend its functionalities, then we can put the additional definition in another file that we create. This is called augmenting
.
另一方面,如果第 3 方库已经包含定义文件.d.ts
,并且我们想要扩展其功能,那么我们可以将附加定义放在我们创建的另一个文件中。这称为augmenting
.
For example:
例如:
// These module declarations are in same file, given that each of them already has their own definition file.
declare module 'events' {
// Extended functionality
}
declare module 'querystring' {
// Extended functionality
}
declare module '...' { ... }
I leave my discovery here just in case somebody has same question. And please correct me if I missed something.
我把我的发现留在这里以防万一有人有同样的问题。如果我错过了什么,请纠正我。
回答by Lee Benson
The actual solution is given in a comment by @Paleo in @hirikarate's answer:
@Paleo 在@hirikarate 的回答中的评论中给出了实际的解决方案:
Imports should be declared insidethe module declaration.
进口应声明里面的模块声明。
Example:
例子:
declare module 'node-helper-lib' {
import * as SomeThirdParty from 'node-helper-lib';
interface Helper {
new(opt: SomeThirdParty.Options): SomeThirdParty.Type
}
export = Helper;
}
回答by G-Wiz
I was getting that error message too. The issue for me was that I was trying to declare another module in an existing type definition file that had a module declaration in it. After I moved the new module declaration to a new file, the error went away.
我也收到了那个错误信息。我的问题是我试图在现有的类型定义文件中声明另一个模块,该文件中有一个模块声明。将新模块声明移至新文件后,错误消失了。
回答by Alexey
In my case I just used following declaration in one of my types files so I was able to use all non-typescript packages:
就我而言,我只是在我的一个类型文件中使用了以下声明,因此我能够使用所有非打字稿包:
declare module '*'