JSDoc to generator dokumentacji API oraz format adnotacji typów dla JavaScript, obsługiwany przez serwery języka TypeScript.
Adnotacje funkcji
/**
* 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);
}
Deklaracje typów i aliasy (@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) {
// ...
}
Popularne znaczniki JSDoc
| Znacznik | Opis | Przykład |
|---|---|---|
@type {Type} |
Deklaruje typ zmiennej | /** @type {string[]} */ |
@param {Type} name |
Dokumentuje parametr funkcji | /** @param {HTMLElement} el */ |
@returns {Type} |
Dokumentuje zwracaną wartość | /** @returns {Promise<void>} */ |
@async |
Wskazuje, że funkcja jest asynchroniczna | /** @async */ |
@deprecated |
Oznacza symbol jako przestarzały | /** @deprecated Use fetchUser() instead */ |
@see |
Odnośnik do zewnętrznej dokumentacji | /** @see {@link https://example.com} */ |
@template T |
Deklaruje generyczny parametr typu | /** @template T @param {T} item */ |