I am working on JavaScript project using VSCode. I am using the UMD
design pattern and vscode intellisense cannot recognize the exports of a module from another file. I added all the declarations in a file called globals.d.ts
. Unfortunately I couldn't find a way to load the globals.d.ts
declarations from my JavaScript files.
export namespace ModuleName { export interface Item { toString(): string; property: string; name: string; } }
(function (global, factory) { "use strict"; if (typeof ModuleName === "undefined" && typeof require === "function") global.ModuleName = require("./mymodule.js"); if (typeof exports !== "undefined" && typeof module !== "undefined") factory(exports); else factory(global.OtherModule = global.OtherModule || {}); })(this, (function (exports) { "use strict"; function myMethod() { } exports.myMethod = myMethod; return exports; }));
I tried using typings install "globals.d.ts"
which created the typings
folder, typings.json
etc. This was only working after opening the typings file in VSCode then closing and reopening the app. That only worked while I kept the typings
file open. This is not a very convenient way to add my interface declarations.
Version: 1.17.0 Shell: 1.7.7 Node: 7.9.0 Architecture: x64
Version: 1.24.1 Shell: 1.7.12 Node: 7.9.0 Architecture: x64
There is no change in behavior.
JavaScript in Visual Studio Code. Visual Studio Code includes built-in JavaScript IntelliSense, debugging, formatting, code navigation, refactorings, and many other advanced language features. Most of these features just work out of the box, while some may require basic configuration to get the best experience.
After installation of the code runner extension, open JavaScript Code in VSCode. Press CTRL+ALT+N shortcut or you may press F1 then write Run Code to run the code.
You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.)
Plain Node.JS solution
Create next files (names should be the same):
lib.js lib.d.ts
Inside lib.js write some code, lets say this one:
function whenDo(params) { return params; } module.exports = whenDo;
Inside lib.d.ts write this:
declare function wd(params: wd.Params): wd.Params; declare namespace wd { interface Params { name: string; } } export = wd;
Then, create somewhere file to consume newly created function and put this code:
const wd = require('./lib/lib'); const opt = wd({name:'drag13'}); console.log(opt.name);
And magic is here, all worked just fine.
Code was stolen from here: https://github.com/Drag13/WhenDo/blob/master/dts/index.d.ts
The approach described here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With