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

Cod*_*rer 6 javascript jsdoc

我想记录一个返回类型取决于提供的参数的函数:

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

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

Cod*_*rer 5

我评论了另一个答案,即我正在寻找的归结为一个通用参数,该参数在 JSDoc 中使用@template. 我不知道它是否是核心 JSDoc 的一部分——我认为它是一个闭包扩展——但是如果你通过 TS 运行你的 JS,或者只是试图让自动完成在你的 IDE 中工作,Typescript 确实可以识别它

改编自该链接:

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