JSDoc中文文档(@use JSDoc)

JSDoc @typedef 标签

目录

语法

@typedef [<type>] <namepath>

概述

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.

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

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

本实例定义了一个更复杂的类型,一个对象的几个属性,并设置其namepath(名称路径) 所以使用该类型将与类一起显示。由于该类型定义实际上不是由类暴露, 习惯上,以记录类型定义作为内部构件。

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