如何使用JSDoc记录ES6类属性

bal*_*ton 6 jsdoc es6-class

我正在使用documentation包,但无法弄清楚如何将其记录到文档类属性中(未通过getter和setter定义)。

如下所示,仅生成了SomeClass的类文档,但是省略了someProperty文档。

/**
 * SomeClass is an example class for my question.
 * @class
 * @constructor
 * @public
 */
class SomeClass {
    constructor () {
        this.someProperty = true  // how do I document this?
    }

    /**
     * someProperty is an example property that is set to `true`
     * @property {boolean} someProperty
     * @public
     */
}
Run Code Online (Sandbox Code Playgroud)

@constructor顺便说一句:jsdoc类上的documentation东西。

Rol*_*olf 10

您可以在类中声明属性,并使用 @type 为其声明类型。在 VSCode 1.70.2 中使用纯 ECMAScript 模块工作。

class Foo {
   /** 
    * Some bary data
    * @type { BarClass }
    */
   bar;
}

let f = new Foo();
f.bar;    // Intellisense can tell you type and purpose
Run Code Online (Sandbox Code Playgroud)


Tx_*_*ter 6

另一种方法是在类文档中声明它们,如下所示:

/**
 * Class definition
 * @property {type} propName - propriety description
 * ...
 */
class ClassName {
  constructor () {...}
  ...
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说不适用于 VSCode intellisense,但 balupton 的解决方案可以。 (7认同)
  • Visual Studio 2019 (16.10) 也不支持此语法。如果确实如此那就太好了,但在那之前,您只需将 JSDoc 信息内联与属性定义混合即可。 (2认同)
  • 在我更新了我的 vs 代码中的某些内容后,它停止工作,我建议转向打字稿以获得良好的工作智能感知。 (2认同)

bal*_*ton 5

将JSDoc移动到首次定义的构造函数中。

/**
 * SomeClass is an example class for my question.
 * @class
 * @constructor
 * @public
 */
class SomeClass {
    constructor () {
        /**
         * someProperty is an example property that is set to `true`
         * @type {boolean}
         * @public
         */
        this.someProperty = true
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定是否有一种方法可以通过不使用将JSDocs内联到构造函数中的方法来完成该documentation程序包。