Search code examples
javascriptnode.jsjsdocjsdoc3documentationjs

How to document an object within a class constructor?


I am currently using JSDoc to attempt to document the following code...

class Test {
  /**
   * @param {Object} raw The raw data.
   */
  constructor(raw) {
    /**
     * Used for things and stuff. It can be useful when referencing Test.myObject.myValue.
     * @type {Object}
     */
    this.myObject = {
      /**
       * This is my string... It does things. Very useful.
       * @type {String}
       */
      myValue: raw.thisIsMyValue
    }
  }
}

But I am not entirely sure how to do it. The example above works in VSCode, but not when generating documentation. I've tried a typedef, but that didn't work as it made it a global typedef instead of being a part of the Test class prototype. How do I even do this?

I know how to define an "anonymous" object for a function, using @param, but I have no idea how to do it for a class prototype. I've been Googling for over an hour and a half now with no luck.


Solution

  • I figured it out

    1. My debugging method wasn't helping, as the theme I was using for my documentation generator was hiding the properties.
    2. When making a typedef, you take out the type right before the object, and then it lists it as properties!

    Example

    class Test {
      /**
       * @param {Object} raw The raw data.
       */
      constructor(raw) {
        /**
         * Used for things and stuff. It can be useful when referencing Test.myObject.myValue.
         * @typedef MyObject
         * @property {String} myValue This is my string... It does things. Very useful.
         */
        this.myObject = {
          myValue: raw.thisIsMyValue
        }
      }
    }