Niall
Niall

Reputation: 30605

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

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; 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.

In the .d.ts, I have the exports listed as;

interface MyI {
    Initiate() : void;
}

module 'modulename' {
    export var i : MyI;
}

I require the module in the client as (.js file);

var i = require("modulename");

In the test code, I require it as (since I stub it through a index.js file);

var i = require("./index.js");

The index.js in turn looks like;

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?

Upvotes: 10

Views: 9477

Answers (1)

Niall
Niall

Reputation: 30605

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. In this case the "main" file is index.js, so the typings become 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.

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. The "typings" value in the package.json should also match the .d.ts file name.

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.

Upvotes: 2

Related Questions