Riferimento rapido

Cheat Sheet per JSDoc 3

Guida completa alle annotazioni JSDoc 3: @param, @returns, @typedef, @type, generici @template, @async e controllo dei tipi in TypeScript.

· Pubblicato

JSDoc è un generatore di documentazione API e un formato di annotazione dei tipi per JavaScript, supportato dai language server TypeScript.

Annotazioni di funzione

/**
 * 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);
}

Dichiarazioni di tipo e 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) {
  // ...
}

Tag JSDoc comuni

Tag Descrizione Esempio
@type {Type} Dichiara il tipo di una variabile /** @type {string[]} */
@param {Type} name Documenta un parametro di funzione /** @param {HTMLElement} el */
@returns {Type} Documenta il valore restituito /** @returns {Promise<void>} */
@async Indica che la funzione è asincrona /** @async */
@deprecated Contrassegna il simbolo come deprecato /** @deprecated Use fetchUser() instead */
@see Collegamento a documentazione esterna /** @see {@link https://example.com} */
@template T Dichiara un parametro di tipo generico /** @template T @param {T} item */

Riferimenti

Argomenti correlati