9
votes

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?

1
Have you looked over setting up VS Code for typescript? code.visualstudio.com/docs/languages/typescriptPaul
I think you mean test.ts, not test.js since the javascript would not have any TS-aware intellisense. And you wouldn't see anything unless you have an index.d.ts file defined. If you changed to index.ts, things would be different, though.Andrew Eisenberg
@AndrewEisenberg. I do mean test.js, vscode uses typescript typings to fuel their intellisense. However, naming the typings index.d.ts solves the intellisense issue, thanks (I previously named it after the package name). Do you have a reference for that? I'm still getting to grips with the typescript stuff and that solution didn't occur to me. Would be able to write it up as an answer?Niall
Interesting. I didn't know that about VS code. I don't use it. I only use sublime text and this is how it would work there. Sublime text would not use TS intellisense in js files AFAIK. I'll write something up.Andrew Eisenberg

1 Answers

2
votes

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.