[英]How to use the .d.ts typings locally for vscode intellisense in a node.js project?
I am setting up a node.js project that uses a native add-on. 我正在设置一个使用本机加载项的node.js项目。 The native add-on includes a large number of exported functions. 本机加载项包括大量导出的功能。 I've setup a typings file ( .d.ts
) that includes all the function definitions and data etc. that are exported from the native add-on. 我已经设置了一个.d.ts
文件( .d.ts
),其中包括从本机加载项导出的所有功能定义和数据等。 When I pack all of this up with npm, and install it into the client project, the vscode intellisense picks up all the types and all is well. 当我用npm打包所有这些内容并将其安装到客户端项目中时,vscode intellisense会选择所有类型,一切都很好。
When I try to use the typings for a test.js
in the same project as the native add-on, the typings are not being picked up, specifically the exported variables; 当我尝试在与本机加载项相同的项目中为test.js
使用类型时,不会选择类型,特别是导出的变量; I suspect it has something to do with the way they are exported in the .d.ts
, or the naming of the module in the .d.ts
. 我怀疑它是与他们在出口的方式.d.ts
,或在模块的命名.d.ts
。
In the .d.ts
, I have the exports listed as; 在.d.ts
,我的出口列为:
interface MyI {
Initiate() : void;
}
module 'modulename' {
export var i : MyI;
}
I require
the module in the client as ( .js
file); 我require
客户端中的模块为( .js
文件);
var i = require("modulename");
In the test code, I require it as (since I stub it through a index.js
file); 在测试代码中,我将其要求为(因为我将其index.js
文件中);
var i = require("./index.js");
The index.js
in turn looks like; index.js
看起来像;
var i = require("./lib/nativeaddon");
module.exports.i = i;
How to I get vscode to use the typings, locally, for the intellisense when I use the add-on (via the index.js
) for the test.js
? 当我将插件(通过index.js
)用于test.js
时,如何让vscode在本地使用intellisense的test.js
?
To create the typings for vscode intellisense to work for both the "local" case (functional with test.js
) and the "global" case (as a node_module), naming the file after the main/entry .js
does the trick. 为了为vscode intellisense创建类型以同时适用于“本地”用例(与test.js
一起test.js
)和“全局”用例(作为node_module),在main / entry .js
完成后命名文件。 In this case the "main"
file is index.js
, so the typings become index.d.ts
. 在这种情况下, "main"
文件是index.js
,因此index.d.ts
变为index.d.ts
。
This does seem natural, but I haven't been able to find the documentation for the vscode intellisense that specifies this as such. 这看起来很自然,但是我无法找到这样指定vscode intellisense的文档。
I had previously named the typings after the package/node_module name, packagename.d.js
) and kept the "main"
(from package.json
) as index.js
. 我以前曾以package / node_module名称packagename.d.js
命名类型,然后将"main"
(来自package.json
)保留为index.js
。 The "typings" value in the package.json
should also match the .d.ts
file name. package.json
中的“ .d.ts
”值也应与.d.ts
文件名匹配。
I suppose a neat alternative to the "index.js" or "main.js", would be to name the main entry point, and the corresponding typings, after the package name. 我想一个很好的替代“ index.js”或“ main.js”的方法是,在包名之后命名主入口点和相应的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.