JSDoc 中文网

语法

¥Syntax

@typedef [<type>] <namepath>

概述

¥Overview

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

¥The @typedef tag is useful for documenting custom types, particularly if you wish to refer to them repeatedly. These types can then be used within other tags expecting a type, such as @type or @param.

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

¥Use the @callback tag to document the type of callback functions.

示例

¥Examples

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

¥This example defines a union type for parameters that can contain either numbers or strings that represent numbers.

使用@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) {
}

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

¥This example defines a more complex type, an object with several properties, and sets its namepath so it will be displayed along with the class that uses the type. Because the type definition is not actually exposed by the class, it is customary to document the type definition as an inner member.

使用 @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) {}