Sub topic 2
Reusable Types with @typedef and @callback
This chapter assumes you can already write a JSDoc block with @param and @returns, and that you know basic type expressions such as string[] and number | null.
Naming Object Shapes and Function Signatures
@typedef gives a name to an object shape. @property lists the fields of that shape. @callback names a function signature. When you name a type once, you can reuse it everywhere and you don't have to repeat long inline types.
/**
* @typedef {Object} User
* @property {string} id - Unique identifier.
* @property {string} name - Display name.
* @property {string} [email] - Optional email address.
*/
/**
* @callback UserPredicate
* @param {User} user
* @returns {boolean}
*/
/**
* Returns the users that match a predicate.
* @param {User[]} users
* @param {UserPredicate} predicate
* @returns {User[]}
*/
function filterUsers(users, predicate) {
return users.filter(predicate);
}A common error is forgetting the square brackets on an optional property. Without them, the type checker reports every object that leaves out email.
Examples, Errors, Links, and Deprecation
These tags describe behavior that the type alone does not show.
| Tag | Purpose |
|---|---|
| `@throws {Type}` | Names an error the function can raise, and when |
| `@example` | Shows a usage sample |
| `@see` / `{@link}` | Points to related code or a URL |
| `@deprecated` | Tells callers to stop using this function |
/**
* Parses a JSON string.
* @param {string} text - The JSON text.
* @returns {unknown} The parsed value.
* @throws {SyntaxError} If the text is not valid JSON.
* @example
* parseJson('{"a": 1}'); // => { a: 1 }
*/
function parseJson(text) {
return JSON.parse(text);
}
/**
* @deprecated Use {@link parseJson} instead.
* @param {string} text
*/
function oldParse(text) {
return parseJson(text);
}Editors draw a line through calls to oldParse, so callers see the deprecation without reading the docs.
Documenting Classes
Put the description above the class and document the constructor's parameters on the constructor itself. To type an instance property, write /** @type {...} */ directly above its assignment.
/** A simple in-memory counter. */
export class Counter {
/** @param {number} [start=0] - Initial value. */
constructor(start = 0) {
/** @type {number} */
this.value = start;
}
/**
* @param {number} [step=1] - Amount to add.
* @returns {number} The new value.
*/
increment(step = 1) {
this.value += step;
return this.value;
}
}Generic Functions with @template
@template T declares a type parameter. The return type then follows the type of the input, so you don't lose it to any.
/**
* @template T
* @param {T[]} items
* @returns {T | undefined}
*/
function first(items) {
return items[0];
}When you call first(["a", "b"]), the result is typed string | undefined.
Exercise
Write a @callback named Comparator that takes two numbers, a and b, and returns a number.
Show answer
/* @callback Comparator @param {number} a @param {number} b @returns {number} / with each tag on its own line inside the block.
Exercise
Why does @property {string} [email] use square brackets?
Show answer
The brackets mark the property as optional, so objects without email still match the User type.