参考在 JSDoc 中输入不同的文件而不导入 [英] Refer to type in different file in JSDoc without importing

查看:19
本文介绍了参考在 JSDoc 中输入不同的文件而不导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio Code 中编写 JavaScript (ES6) 代码并启用 VSCode 的类型检查,如

I'm writing JavaScript (ES6) code in Visual Studio Code and enabled VSCode's type checking as explained in the VSCode docs.

When referring to a type that is defined in another file (Track in the example below), I get an error like [js] Cannot find name 'Track' at the JSDoc reference to that type unless I import it. When I import this type, I get an error from ESLint: [eslint] 'Track' is defined but never used. (no-unused-vars)

I don't want to disable the ESLint rule. Is there a way to import the type only for the type checks in VSCode?

import Track from './Track';

export default class TrackList {

  /**
   * Creates a new track list.
   * @param {Iterable<Track>} tracks the tracks to include
   */
  constructor(tracks) {
    this._tracks = tracks ? Array.from(tracks) : [];
  }
...

解决方案

I have a similar problem and here is how I solved it.

//file.d.ts
export interface Foo {
    bar: number;
}

export as namespace Baz;

Doing this will make the Baz namespace global so you can use it anywhere without importing

/**
 * @param {Baz.Foo} arg 
 */
function test(arg) {

}

Now you get intellisense and type checking in VSCode.

这篇关于参考在 JSDoc 中输入不同的文件而不导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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