Snabbreferens

JSDoc 3 fusklapp

Omfattande referens för JSDoc 3-annoteringar: @param, @returns, @typedef, @type, @template-generik, @async och TypeScript-typkontroll.

· Publicerad

JSDoc är en generator för API-dokumentation och ett typannoteringsformat för JavaScript med stöd i TypeScript-språkservrar.

Funktionsannoteringar

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

Typdeklarationer & 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) {
  // ...
}

Vanliga JSDoc-taggar

Tagg Beskrivning Exempel
@type {Type} Deklarerar typen för en variabel /** @type {string[]} */
@param {Type} name Dokumenterar en funktionsparameter /** @param {HTMLElement} el */
@returns {Type} Dokumenterar returvärdet /** @returns {Promise<void>} */
@async Anger att funktionen är asynkron /** @async */
@deprecated Markerar symbol som föråldrad /** @deprecated Use fetchUser() instead */
@see Länk till extern dokumentation /** @see {@link https://example.com} */
@template T Deklarerar en generisk typparameter /** @template T @param {T} item */

Referenser

Relaterade ämnen