50

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.

Kirkland
  • 2,319
  • 4
  • 20
  • 27

5 Answers5

65

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;
}
mash
  • 2,466
  • 11
  • 17
  • This is similar to PHPDoc: description, followed by `@var type`. Conversely, in both PHPDoc and JSDoc (AFAIK), function parameters are annotated the other way round: type first, then description. – Jake Jan 29 '23 at 00:37
34

one liner:

  /** @type {string} */
  let Y = 'abc';
vitaliytv
  • 694
  • 7
  • 9
  • 1
    When answering an older question it is useful to explain what new aspect of the question your answer addresses. This answer doesn't say anything about what was said in the accepted answer, or indicate if the passage of time has changed the situation due to new versions of software coming out. – Jason Aller Jan 11 '20 at 07:33
  • 2
    This should be marked as the correct answer. To answer @vitaliytv and expand on the answer, yes it seems JS doc have expanded their support for inner members. This works for me. – viztastic Jun 08 '20 at 04:44
  • This is not the correct answer. The OP specifically asked how to include a description along with the type annotation. This does not answer that main question at all. – Jake Jan 29 '23 at 00:39
9

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.

Kirkland
  • 2,319
  • 4
  • 20
  • 27
3

Best thing that worked for me:

/**
  * @name AssetAutoGenerationOption
  * @type {"all" | "master" | "off"}
  */
export type AssetAutoGenerationOption = "all" | "master" | "off";
vljs
  • 978
  • 7
  • 15
0

You might use:

/**
 * @function
 * @property {number} x - Need to explain the purpose of X here.
 * @property {number} y - Need to explain the purpose of Y here.
 * @returns {number} - Describe return value here (assumed number type for this example)
 */
function example() {
  var x
  var y = 'abc';
  return z;
}

You can also examine the official documentation here: https://jsdoc.app/about-getting-started.html#:~:text=JSDoc's%20purpose%20is%20to%20document,before%20the%20code%20being%20documented.

Anthony
  • 13,434
  • 14
  • 60
  • 80
Zippy
  • 3,826
  • 5
  • 43
  • 96