hir*_*ate 57 node.js typescript typescript-typings typescript2.1
我找不到@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;
}
Run Code Online (Sandbox Code Playgroud)
但是,Visual Studio代码不断产生此错误并将红线置于declare module 'node-helper-lib':
[ts]增强中的模块名称无效.模块'node-helper-lib'解析为'{project path} \node_modules \node-helper-lib\index.js'中的无类型模块,该模块无法扩充.
这是不合法的,因为库是无类型的,所以我应该被允许添加输入?
更新:
我在用:
Lee*_*son 92
实际解决方案由@Paleo在@hirikarate的回答中给出评论:
进口应声明里面的模块声明.
例:
declare module 'node-helper-lib' {
import * as SomeThirdParty from 'node-helper-lib';
interface Helper {
new(opt: SomeThirdParty.Options): SomeThirdParty.Type
}
export = Helper;
}
Run Code Online (Sandbox Code Playgroud)
hir*_*ate 34
经过一些尝试和错误后,我发现这augmentation意味着"在与其他模块声明相同的文件中声明一个模块".
因此,如果我们要为无类型的第三方JavaScript库编写定义文件,那么我们必须declare module 'lib-name'在该文件中只有一个,并且'lib-name'必须与库名完全匹配(可以在其package.json中找到,"名称"财产".
另一方面,如果第三方库已经包含定义文件 .d.ts,并且我们想要扩展其功能,那么我们可以将附加定义放在我们创建的另一个文件中.这叫做augmenting.
例如:
// 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 '...' { ... }
Run Code Online (Sandbox Code Playgroud)
我在这里留下我的发现,以防有人有同样的问题.如果我错过了什么,请纠正我.
| 归档时间: |
|
| 查看次数: |
13300 次 |
| 最近记录: |