我们有在节点上的 JS VM 调用中评估的脚本,它声明了一些上下文全局变量,如下面的代码所示。问题是,在开发时,当开发人员在工作时some-script.js
,我们如何让 VSCode 在aGlobalThing
引用该文件时向该文件的作者提供智能感知?
项目本身同时支持 TS 和 JS,但是脚本必须用 JS 编写,原因不相关。是否有某种注释头可以放在每个脚本文件的顶部,这会给 VSCode 足够的提示,它可以理解aGlobalThing
变量已经存在IVmGlobals
类型信息?typedef,全局注释,一些带有嵌入式但注释包装导入的组合?
// code-runner.ts
export interface IVmGlobals {
aGlobalThing: {
a: number;
b: string;
};
};
export async function runCode(codeSnippet) {
const context = vm.createContext({
aGlobalThing: { a: 123, b: 'xyz', },
} as IVmGlobals);
await vm.runInContext(codeSnippet, context);
}
// some-script.js
// will assign 123, but how can a developer get
// intellisense while writing this file that indicates
// the presence of aGlobalThing as existing, and having
// a member called `a` that holds number?
const a = aGlobalThing.a;
//... more code, etc.