Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom typings file in a JavaScript VSCode project

Problem

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.

Example Module declaration

export namespace ModuleName {     export interface Item {         toString(): string;         property: string;         name: string;     } } 

Example JavaScript File

(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; })); 

What I tried

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.

About VSCode (8 months ago)

Version: 1.17.0 Shell: 1.7.7 Node: 7.9.0 Architecture: x64 

About VSCode (Now)

Version: 1.24.1 Shell: 1.7.12 Node: 7.9.0 Architecture: x64 

There is no change in behavior.

like image 320
nick zoum Avatar asked Oct 11 '17 02:10

nick zoum


People also ask

Can you program JavaScript in Visual Studio code?

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.

How do I practice JavaScript in Visual Studio code?

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.

How do I add IntelliSense code to Visual Studio?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.)


1 Answers

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. enter image description here

Code was stolen from here: https://github.com/Drag13/WhenDo/blob/master/dts/index.d.ts

The approach described here

like image 79
Drag13 Avatar answered Sep 21 '22 18:09

Drag13