Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JSDoc to document an ES6 class property

Tags:

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.

like image 709
balupton Avatar asked Jul 22 '18 17:07

balupton


People also ask

How to write JSDoc for class?

As with Java, to document a function, variable or class. You just have to create a comment before the element you want to document. The only thing you have to keep in mind is that the comment needs to start with /** and end with */ , like so: /** * This is a JSDoc comment * ...

Can you use JSDoc with TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.

What are JSDoc tags?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.

What is JSDoc comment?

JSDoc comments are used for documentation lookup with Ctrl+Q in JavaScript and TypeScript, see JavaScript documentation look-up and TypeScript documentation look-up, as well as for type annotations and method return type hints in chained methods.


1 Answers

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.

like image 105
balupton Avatar answered Oct 27 '22 01:10

balupton