Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct JSDoc syntax for a local variable?

For a function like this...

function example() {
  var X = 100;

  ...

  var Y = 'abc';

  ...

  return Z;
}

I need to explain the purpose of some of the local variables. Adding a description like this...

function example() {
  /**
   * @description - Need to explain the purpose of X here.
   */
  var X = 100;

  ...

  /**
   * @description - Need to explain the purpose of Y here.
   */
  var Y = 'abc';

  ...

  return Z;
}

...doesn't seem to be getting picked up by JS Doc v3.4.0.

What is the correct syntax?

P.S. Some of my use cases call for multi-line comments.

like image 366
Kirkland Avatar asked Aug 01 '16 21:08

Kirkland


People also ask

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.

How do you document an object in JSDoc?

If a parameter is expected to have a specific property, you can document that property by providing an additional @param tag. For example, if an employee parameter is expected to have name and department properties, you can document it as follows: /** * Assign the project to a list of employees.

Why is JSDoc used?

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.

Can I use JSDoc in 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.


3 Answers

I usually use something like the code below in my projects.

function example() {   /**    * Need to explain the purpose of X here.    * @type {number}    */   var X = 100;    ...    /**    * Need to explain the purpose of Y here.    * @type {string}    */   var Y = 'abc';    ...    return Z; } 
like image 69
mash Avatar answered Oct 09 '22 18:10

mash


one liner:

  /** @type {string} */   let Y = 'abc'; 
like image 34
vitaliytv Avatar answered Oct 09 '22 18:10

vitaliytv


It seems that JS Docs ignores comments within the "block" (E.g. class, function, etc.). I tried...

@description
@inner
@instance
@member
@memberof
@name
@summary

...and others. I was unable to get any of them to generate documentation. Throughout the JS Doc examples they use normal JS comments for this sort of thing.

I have concluded that there is no official JS Doc syntax for this.

like image 26
Kirkland Avatar answered Oct 09 '22 18:10

Kirkland