【问题标题】:How to document interface key type with JSDoc?如何使用 JSDoc 记录界面键类型?
【发布时间】:2022-06-28 11:49:27
【问题描述】:

假设我有一个这样的界面:

interface Foo {
  [barKey: string]: boolean;
}

// or as a type

type Foo = {
  [barKey: string]: boolean;
}

然后我用这个类型初始化一个变量:

const myObj: Foo = {
  "hello": false,
  "world": true,
}

当我将鼠标悬停在它上面时,如何让编辑器(VSCode/IntellIJ/任何支持 JSDoc 的)拾取 JSDoc 作为键?我尝试过这样的事情:

/**
 * A cool interface
 */
interface Foo {
  /**
   * Some hints about the property
   */
  [barKey: string]: boolean;
}

不幸的是,这不起作用,我也尝试过这样的:

/**
 * A cool interface
 */
interface Foo {
  [
    /**
     * Some hints about the property
     */
    barKey: string
  ]: boolean
}

但是没有成功,有什么办法可以做到吗?

TL;DR:如果从指定键类型的接口派生,如何记录每个对象键,以便编辑器在将鼠标悬停在键上时拾取它?

【问题讨论】:

    标签: typescript jsdoc


    【解决方案1】:

    我想这必须取决于您的 IDE/编辑器。您的第一个示例在 WebStorm 2022.1.2 中完美运行。

    我想另一种方法是在界面定义中仅包含键作为项目符号。这样,当您将鼠标悬停在上面时,您至少可以得到它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2018-12-30
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 2015-02-05
      相关资源
      最近更新 更多