Guideline for JSDoc

Guideline for JSDoc

2023-02-24
4 min read

Overview

For role of type checker Typescript is a cool way. However, in some cases, it's excessive. The codes should be compiled whenever codes has been changed. It means that it doesn't require extra dependencies. Because of some reasons, some projects are only using JSDoc. Moreover, many IDE such as WebStorm and VSCode supports type checker. JsDoc comments should be beginning with /* or /**.

Common tags

Keyword starts with "@". Following table describes commonly using keywords. This article will not display examples about all tags, but click the link to see more tags and details.

TagsDescription
@paramparameter values for function
@returnreturn value
@typetype of variable
@constconst of variable
@typedefcustom types
@propertyproperties of class
@linklink to another item in the documentation.
@throwswhat errors could be thrown.
@aliastreat a member as if it had a different name
@namethe name of an object.
@privateprivate variable or function
@deprecateddocument that this is no longer the preferred way.
@authorthe author of function, class etc
@constructsindicates this function is constructor
@readonlyread-only field, function variable
@todotask to be completed
@enuma collection of related properties.
@overridea symbol overrides its parent.

@param and @return

The parameter may also be declared optional by surrounding the name with square brackets . @param can be three types

  1. Primitive (string, number ...)
  2. global or imported
  3. Declared with typedef

@param tag provides the name, type and description in turn

js
/**
 * @param {string=} n - Optional
 * @param {string} [n] - Optional
 * @param {(string|number)} n - Multiple types
 * @param {*} n - Any type
 * @param {...string} n - Repeatable arguments
 * @param {string} [n="hi"] - Optional with default
 * @param {string[]} n - Array of strings (Type is changeable)
 */

@return keyword can indicate types surrounding the name with square brackets

js
/**
 * @return {Promise<string[]>} n - Promise fulfilled by array of strings
*/

Example 1 - Normal function

js
/**
 * This is exmaple function
 * @param {string} a - Example string param
 * @param {string | number } b - Example
 * @param {string} [c="hello"] c - string paramter with default "hello"
 * @return {*} any type can be returned
 */
function exampleFun(a, b, c) {
  // ...
  return d
}

Example 2 - Promise

js
/**
 * This is exmaple function
 * @return {Promise<number>} return prmosie
 */
async function exampleFun() {
  return 100
}

@type, @typedef, and @property

@type tag uses the same type syntax as @param, but there is no parameter name. @typedef tag is useful for documenting custom types, particularly if you wish to refer to them repeatedly

Example for @type

js
/** @type {(string)} */
var foo
/** @type {number} */
var bar = 1

Example 1 - @type and @property

js
/**
 * Read a book taken by parameter
 * @typedef {Object} Book
 * @property {string} title - The title
 * @property {string} author - The author
 */
function read(book) {}

Example 2 - importing types

js
/**
 * @typedef {import('./Foo').default} Foo
 * @param {Foo} foo
 */
function fooFunc(foo) {}
js
/**
 * Read a book taken by parameter
 * @param {Book} song - The {@link Book} to be read
 */
function read(book) {}

@private, @override and @constructor

The @private, @override, and @constructor tags are usually used in class. There is not only @prviate but also more property modifiers such as @public and @protected.

Example

js
/**
 * @class
*/
class Product {
  /**
   * @constructor
   */
  constructor() {
    /** @private */
    this.name = 0;
    /** @protected */
    this.price = 0;
    /** @public */
    this.description = 0;
  }
}

Example - @override

js
export class P {
  exampleFunc() { }
}
class C extends P {
  /** @override */
  exampleFunc() { }
}

Other examples

Shortly mention some examples about some tags.

@const

@const typically represents it's constant variable.

js
/** @const {number} */
const num = 1

@readonly

@const indicates the properties won't be modified with any reason.

js
/** @readonly */
class Product {
  constructor() {
    /** @readonly */
    this.name = 0
  }
}

@author and @deprecated

@author indicates who makes this item. @deprecated indicates this item is no longer usable.

js
/**
 * @author it's me
 * @deprecated
 */
function exampleFunc() {}

@enum

@enum tag allows to create a static properties consisting of same type.

js
/** @enum {number} */
const VideoStatus = {
  LOAD: 0,
  PLAYING: 1,
  PAUSE: 2,
};

@alias and @name

@alias tag and @name play a similar role for renaming

js
/**
 * a function.
 * @name b
 */
function a() {}

/**
 * a function.
 * @alias b
 */
function a() {}

@todo

js
/** @todo - Add an example logic */
function exampleFunc() {}

Ref

Free open source project made by Youngjin