编写自定义TypeScript定义文件时,出现错误“模块'名称'解析为无类型模块...”

时间:2022-03-09 21:59:16

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:

我找不到我安装的NodeJS软件包的TypeScript定义@type / {name},因此我尝试为其编写一个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代码不断产生此错误,并在声明模块'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
  • TypeScript:2.1.4
  • Visual Studio Code: 1.9.1
  • Visual Studio代码:1.9.1
  • Node JS: 6.9.4
  • 节点JS:6.9.4
  • Windows 10 x64
  • Windows 10 x64

2 个解决方案

#1


24  

The actual solution is given in a comment by @Paleo in @hirikarate's answer:

实际解决方案由@Paleo在@hirikarate的回答中给出评论:

Imports should be declared inside the 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;
}

#2


13  

After some tries and errors, I found that augmentation means "declaring a module in the same file with other module declaration(s)".

经过一些尝试和错误后,我发现扩充意味着“在与其他模块声明相同的文件中声明一个模块”。

Therefore if we want to write definition file for an untyped 3rd-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).

因此,如果我们要为无类型的第三方JavaScript库编写定义文件,那么我们必须在该文件中只有一个声明模块'lib-name',并且'lib-name'必须与库名完全匹配(可以在它的package.json,“name”属性)。

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.

另一方面,如果第三方库已经包含定义文件.d.ts,并且我们想要扩展其功能,那么我们可以将附加定义放在我们创建的另一个文件中。这称为扩充。

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 miss something.

我在这里留下我的发现,万一有人有同样的问题。如果我错过了什么,请纠正我。

#1


24  

The actual solution is given in a comment by @Paleo in @hirikarate's answer:

实际解决方案由@Paleo在@hirikarate的回答中给出评论:

Imports should be declared inside the 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;
}

#2


13  

After some tries and errors, I found that augmentation means "declaring a module in the same file with other module declaration(s)".

经过一些尝试和错误后,我发现扩充意味着“在与其他模块声明相同的文件中声明一个模块”。

Therefore if we want to write definition file for an untyped 3rd-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).

因此,如果我们要为无类型的第三方JavaScript库编写定义文件,那么我们必须在该文件中只有一个声明模块'lib-name',并且'lib-name'必须与库名完全匹配(可以在它的package.json,“name”属性)。

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.

另一方面,如果第三方库已经包含定义文件.d.ts,并且我们想要扩展其功能,那么我们可以将附加定义放在我们创建的另一个文件中。这称为扩充。

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 miss something.

我在这里留下我的发现,万一有人有同样的问题。如果我错过了什么,请纠正我。