项目信息
我正在开发一个 JavaScript 项目,该项目使用.d.ts
文件。这是我之前提出的问题的后续问题,因此您可以查看有关该项目的更多信息here.
Problem
尽管我通常可以从类型文件中提取函数,但我无法提取空的或纯粹由接口组成的接口或命名空间。我通过创建一个临时解决了这个问题const
每个接口的实现和使用@typeof ConstantImplementation
在评论中。请参见下面的示例:
// Typings File
export namespace test {
export interface ITest {
foo: string;
bar: number;
}
export const Test: ITest;
}
// 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`
我想知道是否有更好的方法来解决这个问题,最好是不会抛出错误的方法(使用eslint ignore line
不是修复)。
澄清
这个问题不是关于从类型文件中获取功能。它纯粹是为了让 VSCode 智能感知与类型接口一起工作。这是一张解释我想要什么的图像(圆圈内的两条线):
所以我能够使用 JSDoc 解决这个问题
测试.d.ts
export namespace test {
export interface ITest {
foo: string;
bar: number;
}
}
test.js
/**
* @type {import("./test").test.ITest}
*/
let x;
x.
智能感知现在可以工作了
我还发现的一件事是,如果你添加jsconfig.json
with
jsconfig.json
{
"compilerOptions": {
"checkJs": true
}
}
您的智能感知进一步提高
Update-1
正如@nickzoum 所指出的,如果您定义test.d.ts
像下面这样
export interface ITest {
foo: string;
bar: number;
}
export as namespace test;
然后你也可以在 JS 中使用下面的形式进行智能感知
/** @typedef {import("./test").ITest} ITest */
/** @type {ITest} */
var x = {};
x.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)