nick zoum
nick zoum

Reputation: 7285

Set global declaration in vscode JavaScript

I'm working on a JavaScript transpiler that apart from other things will also replace certain functions and variables upon build.

For example the following file (./src/my-module.js):

defineModule("MyModule", function(exports) {
  return exports;
});

Will be copied and converted to (./build/my-module.js):

(function(global, factory) {
  "use strict";
  if (typeof exports !== "undefined" && typeof module !== "undefined") module.exports.MyModule = factory(exports.MyModule || {});
  else factory(global.MyModule = {});
})(this, function(exports) {
  return exports;
});

Some of these functions could also return a result. In that case I would like to be able to declare the types of the parameters and the result of the function without using require. Is it possible to have a global .d.ts definition in VSCode?

So far, all I've done is add the functions to the global variable of eslint so as to not have errors.

Upvotes: 11

Views: 5305

Answers (2)

nick zoum
nick zoum

Reputation: 7285

Acknowledgment

This answer was mostly inspired by mootrichard's answer, but since it had to be modified, to work with my project, I am also adding this answer.

Solution

If you press F12 on a global JavaScript function (i.e. eval) a typing declarations file will appear (lib.es5.d.ts), containing JavaScript documentation. You can just add any extra namespaces or function to that file. But you need to use declare and not export.

Example:

//... Other Global JavaScript Declarations

// JavaScript Number Interface
interface Number {
  //...
}

// JavaScript Date Interface
interface Date {
  //...
}

declare function ezDefine(moduleName: string, generator: *): void;

Upvotes: 1

mootrichard
mootrichard

Reputation: 3611

You can specify your own TypeScript folder path in your settings.json where you can specify your own lib.*.d.ts files using the typescript.tsdk option.

{
  "typescript.tsdk": "node_modules/typescript/lib"
}

Upvotes: 7

Related Questions