我正在使用VSCode开发JavaScript项目.我正在使用UMD设计模式,而vscode intellisense无法识别来自另一个文件的模块的导出.我在一个名为的文件中添加了所有声明globals.d.ts.不幸的是我找不到globals.d.ts从JavaScript文件加载声明的方法.
export namespace ModuleName {
export interface Item {
toString(): string;
property: string;
name: string;
}
}
Run Code Online (Sandbox Code Playgroud)
(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;
}));
Run Code Online (Sandbox Code Playgroud)
我正在开发一个利用.d.ts文件的JavaScript项目。这是我先前提出的问题的后续问题,因此您可以在此处查看有关该项目的更多信息。
尽管我通常可以从类型文件中提取函数,但是我不能提取为空或仅由接口组成的接口或名称空间。我已经通过const为每个接口创建一个实现并@typeof ConstantImplementation在注释中使用来临时解决了此问题。请参见下面的示例:
// Typings File
export namespace test {
export interface ITest {
foo: string;
bar: number;
}
export const Test: ITest;
}
Run Code Online (Sandbox Code Playgroud)
// JS File
if (undefined) var {Test: ITest} = require("globals.d.ts").test;
// Above line shows `unused var error`
/* @type {typeof ITest} */
var x = {};
x.foo = "hello";
x.bar = 3;
// if I do `x.` intellisense should suggest `foo` and `bar`
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来解决该问题,最好是一种不会抛出错误(使用eslint ignore …