在 vscode JavaScript 中设置全局声明 [英] Set global declaration in vscode JavaScript

查看:38
本文介绍了在 vscode JavaScript 中设置全局声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 JavaScript 转译器,除了其他功能外,它还会在构建时替换某些函数和变量.

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

例如以下文件(./src/my-module.js):

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

将被复制并转换为(./build/my-module.js):

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

其中一些函数也可以返回结果.在这种情况下,我希望能够在不使用 require 的情况下声明参数的类型和函数的结果.VSCode 中是否可以有一个全局的 .d.ts 定义?

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?

到目前为止,我所做的只是将函数添加到eslint的全局变量中,以免出错.

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

推荐答案

致谢

这个答案的灵感主要来自 mootrichard 的答案,但由于必须对其进行修改才能与我的项目一起使用,因此我也添加了此答案.

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.

如果您在全局 JavaScript 函数(即 eval)上按 F12,则会出现一个类型声明文件(lib.es5.d.ts>),包含 JavaScript 文档.您可以向该文件添加任何额外的命名空间或函数.但是你需要使用 declare 而不是 export.

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.

示例:

//... Other Global JavaScript Declarations

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

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

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

这篇关于在 vscode JavaScript 中设置全局声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆