@use JSDoc

语法

@typedef [<type>] <namepath>

概述

@typedef 标记对于记录自定义类型非常有用,特别是如果您希望反复引用它们。然后,这些类型可以在其他标记中使用,这些标记需要一个类型,例如 @type@param

使用 @callback 标记记录回调函数的类型。

示例

此示例定义了一个联合类型,用于可以包含数字或表示数字的字符串的参数。

使用 @typedef 标记
/**
 * A number, or a string containing a number.
 * @typedef {(number|string)} NumberLike
 */

/**
 * Set the magic number.
 * @param {NumberLike} x - The magic number.
 */
function setMagicNumber(x) {
}

此示例定义了一个更复杂的类型,一个具有多个属性的对象,并设置其名称路径,以便将其与使用该类型的类一起显示。由于类实际上并未公开类型定义,因此通常将类型定义记录为内部成员。

使用 @typedef 记录类的复杂类型
/**
 * The complete Triforce, or one or more components of the Triforce.
 * @typedef {Object} WishGranter~Triforce
 * @property {boolean} hasCourage - Indicates whether the Courage component is present.
 * @property {boolean} hasPower - Indicates whether the Power component is present.
 * @property {boolean} hasWisdom - Indicates whether the Wisdom component is present.
 */

/**
 * A class for granting wishes, powered by the Triforce.
 * @class
 * @param {...WishGranter~Triforce} triforce - One to three {@link WishGranter~Triforce} objects
 * containing all three components of the Triforce.
 */
function WishGranter(triforce) {}