Kurzreferenz

JSDoc 3 Spickzettel

Umfassende Referenz für JSDoc 3 Annotationen: @param, @returns, @typedef, @type, @template Generics, @async und TypeScript-Typprüfung.

· Veröffentlicht

JSDoc ist ein API-Dokumentationsgenerator und ein Format für Typannotationen in JavaScript, das von TypeScript-Language-Servern unterstützt wird.

Funktionsannotationen

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

Typdeklarationen & Aliase (@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) {
  // ...
}

Häufige JSDoc-Tags

Tag Beschreibung Beispiel
@type {Type} Deklariert den Typ einer Variablen /** @type {string[]} */
@param {Type} name Dokumentiert einen Funktionsparameter /** @param {HTMLElement} el */
@returns {Type} Dokumentiert den Rückgabewert /** @returns {Promise<void>} */
@async Kennzeichnet eine Funktion als asynchron /** @async */
@deprecated Markiert Symbol als veraltet /** @deprecated Use fetchUser() instead */
@see Verweist auf externe Dokumentation /** @see {@link https://example.com} */
@template T Deklariert einen generischen Typparameter /** @template T @param {T} item */

Referenzen

Verwandte Themen