Referencia rápida

Hoja de trucos de JSDoc 3

Guía completa de anotaciones JSDoc 3: @param, @returns, @typedef, @type, genéricos @template, @async y verificación de tipos con TypeScript.

· Publicado

JSDoc es un generador de documentación de APIs y formato de anotación de tipos para JavaScript, compatible con servidores de lenguaje TypeScript.

Anotaciones de funciones

/**
 * Calculates total invoice sum including tax.
 *
 * @param {number} subtotal - Net amount before tax.
 * @param {number} [taxRate=0.1] - Optional tax percentage (defaults to 10%).
 * @param {string} [currency='USD'] - Three-letter ISO currency code.
 * @returns {number} The calculated total invoice price.
 * @throws {RangeError} When subtotal is negative.
 */
function calculateTotal(subtotal, taxRate = 0.1, currency = 'USD') {
  if (subtotal < 0) throw new RangeError('Subtotal must be positive');
  return subtotal * (1 + taxRate);
}

Declaraciones de tipo y alias (@typedef)

/**
 * @typedef {Object} UserProfile
 * @property {number} id - Unique numeric identifier.
 * @property {string} username - User account handle.
 * @property {string} [email] - Optional contact email address.
 * @property {'admin' | 'editor' | 'member'} role - Access control role.
 */

/**
 * Loads user record from database.
 * @param {number} id
 * @returns {Promise<UserProfile>}
 */
async function getUser(id) {
  // ...
}

Etiquetas JSDoc comunes

Etiqueta Descripción Ejemplo
@type {Type} Declara el tipo de una variable /** @type {string[]} */
@param {Type} name Documenta un parámetro de función /** @param {HTMLElement} el */
@returns {Type} Documenta el valor de retorno /** @returns {Promise<void>} */
@async Indica que la función es asíncrona /** @async */
@deprecated Marca un símbolo como obsoleto /** @deprecated Use fetchUser() instead */
@see Enlace a documentación externa /** @see {@link https://example.com} */
@template T Declara un parámetro de tipo genérico /** @template T @param {T} item */

Referencias

Temas relacionados