我可以在 JSDoc @return 中引用参数类型吗?

Can I reference a param type in JSDoc @return?

我想记录一个函数,其 return 类型取决于提供的参数:

/**
* @param {*} x A thing
* @return {????} The thing in an array
*/
function arrayOf(x) { return [x]; }

是否可以命名或以其他方式引用函数参数的实际类型,以便我可以使用它来指定 return?

不知道是否有通用的表示方式 - 这需要在功能描述中指定,但以下可能是选项:

为了详细描述,可能会创建一个东西 typedef:

/**
 * Thing Type
 * @typedef {*} thing
 */

/**
 * Returns a single element array encapsulating only the param value
 * @param {thing} x A thing
 * @return {thing[]} The thing in an array
 */
function arrayOf(x) { return [x]; }

否则,简单地说,

/**
 * Returns a single element array encapsulating only the param value
 * @param {*} x A thing
 * @return {*[]} The thing in an array
 */
function arrayOf(x) { return [x]; }

我在另一个答案上评论说我正在寻找的东西归结为一个通用参数,它是在 JSDoc 中使用 @template 实现的。我不知道它是否是核心 JSDoc 的一部分——我认为它是一个 Closure 扩展——但是 Typescript does recognize it 如果你是 运行 你的 JS 通过 TS,或者只是试图让自动完成工作在你的IDE.

改编自 link:

/**
 * @template T
 * @param {T} x - A generic parameter that flows through to the return type
 * @return {T[]}
 */
function id(x) {
  return [x];
}