Sub topic 4
Reusable Types with @typedef and @callback
This chapter assumes you can already write @param and @returns tags and read JSDoc type expressions such as string[], number | null, and { id: string }. It also assumes you know JavaScript objects and array methods like filter.
Inline object types work for one function. When the same shape shows up in five signatures, copying it five times invites drift: someone adds a field in one place and forgets the others. @typedef and @callback let you name a type once and refer to it everywhere.
Naming an object shape with @typedef and @property
@typedef declares a named type. For objects, you pair it with one @property tag per field. Property tags use the same syntax as @param: a type in braces, a name, an optional description, and square brackets to mark a field as optional.
/**
* @typedef {Object} User
* @property {string} id - Unique identifier.
* @property {string} name - Display name.
* @property {string} [email] - Optional email address.
*/A typedef comment documents no code of its own. It stands alone, usually near the top of a file, and it produces no JavaScript at runtime. After you declare it, User works as a type name in any JSDoc type expression, for example {User}, {User[]}, or {User | null}.
Nested properties
For nested objects, use dotted names under a parent property:
/**
* @typedef {Object} Order
* @property {string} id
* @property {Object} shipping
* @property {string} shipping.city
* @property {string} shipping.postcode
*/The compact form
You can also write the whole shape as a type literal on one line:
/** @typedef {{ id: string, name: string, email?: string }} User */This form is shorter, but it leaves no room for a description of each field. Use the @property form when the fields need explanations.
Naming a function signature with @callback
@callback does for function types what @typedef does for objects. Inside the block, @param and @returns describe the signature that the function must follow.
/**
* @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);
}
/** @type {User[]} */
const users = [
{ id: "1", name: "Ada", email: "[email protected]" },
{ id: "2", name: "Linus" },
];
/** @type {UserPredicate} */
const hasEmail = (user) => user.email !== undefined;
console.log(filterUsers(users, hasEmail)); // [ { id: '1', name: 'Ada', email: '[email protected]' } ]The @type {UserPredicate} annotation on hasEmail lets TypeScript-aware editors infer that user is a User without a separate @param tag.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Putting `@typedef` in the comment block of a function | The type can get attached to the wrong symbol or be ignored | Give the typedef its own comment block |
| Writing `@property` with no type | Editors treat the field as `any` | Always write `{type}` |
| Writing a lowercase `{object}` | Some tools treat it differently from `{Object}` | Use `{Object}` with `@property` tags, as the jsdoc tool expects |
| Writing an arrow type such as `@typedef {(u: User) => boolean}` for the jsdoc HTML tool | TypeScript accepts it, but the jsdoc generator may fail to parse it | Use `@callback` when you also generate HTML docs |
| Declaring the same type name twice | Duplicate-identifier errors or one declaration silently winning | Define each type once, then import it (covered in a later chapter) |
Exercise
Write a @typedef for a Product with a required string sku, a required number price, and an optional array of strings tags. Then write a @callback named PriceFormatter that takes a Product and returns a string.
Show answer
/ @typedef {Object} Product @property {string} sku - Stock-keeping unit. @property {number} price - Price in the store currency. @property {string[]} [tags] - Optional labels. */ / @callback PriceFormatter @param {Product} product @returns {string} /
Exercise
A teammate writes @typedef {(p: Product) => string} PriceFormatter and the editor accepts it, but running the jsdoc HTML generator fails. Why, and what should they use instead?
Show answer
The arrow-function type is TypeScript syntax. Editors understand it, but the jsdoc tool's type parser may not. Use a @callback block with @param and @returns, which both tools support.