Cheat Sheet速查表
跳转到主要内容

Jsdoc 速查表

本文根据 Devhints / Jsdoc 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2024-07-26。

函数

/**
 * This is a function.
 *
 * @param {string} n - A string param
 * @param {string} [o] - A optional string param
 * @param {string} [d=DefaultValue] - A optional string param
 * @return {string} A good string
 *
 * @example
 *
 *     foo('hello')
 */

function foo(n, o, d) {
  return n
}

参见: https://jsdoc.app/index.html

类型

类型 (Type)说明
@param {string=} n可选
@param {string} [n]可选
@param {(string|number)} n多种类型
@param {*} n任意类型
@param {...string} n可重复参数
@param {string} [n="hi"]可选(带默认值)
@param {string[]} n字符串数组
@return {Promise<string[]>} n由字符串数组提供的 Promise

参见: https://jsdoc.app/tags-type.html

变量

/**
 * @type {number}
 */
var FOO = 1
/**
 * @const {number}
 */
const FOO = 1

类型定义 (Typedef)

/**
 * A song
 * @typedef {Object} Song
 * @property {string} title - The title
 * @property {string} artist - The artist
 * @property {number} year - The year
 */
/**
 * Plays a song
 * @param {Song} song - The {@link Song} to be played
 */

function play(song) {}

参见: https://jsdoc.app/tags-typedef.html

类型定义简写

/**
 * A song
 * @typedef {{title: string, artist: string, year: number}} Song
 */
/**
 * Plays a song
 * @param {Song} song - The {@link Song} to be played
 */

function play(song) {}

参见: https://jsdoc.app/tags-typedef.html

导入类型

/**
 * @typedef {import('./Foo').default} Bar
 */

// or

/** @import { Bar } from "./Foo.js" */

/**
 * @param {Bar} x
 */

function test(x) {}

此语法是 TypeScript 特有的.

其他关键字

/**
 * @throws {FooException}
 * @async
 * @private
 * @deprecated
 * @see
 *
 * @function
 * @class
 */

查看完整列表: https://jsdoc.app/index.html#block-tags

重命名

/**
 * @alias Foo.bar
 * @name Foo.bar
 */

优先使用 alias 而非 name。请参阅: https://jsdoc.app/tags-alias.html