Set global declaration in vscode JavaScript

僤鯓⒐⒋嵵緔 提交于 2021-02-18 20:52:08

问题


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.


回答1:


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"
}



回答2:


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;


来源:https://stackoverflow.com/questions/56866852/set-global-declaration-in-vscode-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!