如何使用 JSDoc 记录 ES6 类属性

问题描述 投票:0回答:3

我正在使用

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
     */
}

旁白:jsdoc 类上的

@constructor
是一个
documentation
的东西

jsdoc es6-class
3个回答
68
投票

someProperty
的 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
    }
}

我不确定是否有一种方法可以通过

documentation
使用不涉及将 JSDocs 内联到构造函数中的方法来完成它。


16
投票

这是在类文档中声明它们的另一种方法:

/**
 * Class definition
 * @property {type} propName - property description
 * ...
 */
class ClassName {
  constructor () {...}
  ...
}

14
投票

您可以在类中声明属性,并使用@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
© www.soinside.com 2019 - 2024. All rights reserved.