39

I'm using the documentation package, but cannot figure out how to get it to document class properties (that aren't defined via getters and setters).

As the following just generates class documentation for SomeClass, but omits the someProperty documentation.

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

An aside: the @constructor on the class jsdoc is a documentation thing.

mkobit
  • 43,979
  • 12
  • 156
  • 150
balupton
  • 47,113
  • 32
  • 131
  • 182

3 Answers3

58

Move the JSDoc for someProperty into the constructor where it is first defined:

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

I'm unsure if there is a way of accomplishing it with the documentation package using an approach that doesn't involve inlining the JSDocs into the constructor.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
balupton
  • 47,113
  • 32
  • 131
  • 182
13

Another way is to declare them in class documentation as follows:

/**
 * Class definition
 * @property {type} propName - propriety description
 * ...
 */
class ClassName {
  constructor () {...}
  ...
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Riccardo Manzan
  • 703
  • 1
  • 6
  • 19
  • 6
    This did not work with VSCode intellisense for me, but balupton's solution did. – mrossman Jun 09 '21 at 13:59
  • 2
    Visual Studio 2019 (16.10) also doesn't support this syntax. Would be nice if it did, but until then, you just have to mix the JSDoc info inline with the property definitions. – Jacob Stamm Jun 10 '21 at 19:47
  • 1
    After i updated something in my vs code, this stopped working, i suggest moving to typescript in order to have a nice working intellisense. – Riccardo Manzan Jul 07 '21 at 09:52
6

You can declare properties in a class, and use @type to declare a type for them. Worked in VSCode 1.70.2 with in a plain ECMAScript Module.

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

let f = new Foo();
f.bar;    // Intellisense can tell you type and purpose
Rolf
  • 730
  • 6
  • 14
  • This work for me especially with class that come from Node packages. Example: `@type {Promise} #conn` for use with **madiadb** package. – vee Jun 07 '23 at 18:58