如何在JSDOC中扩展typedef参数?

dud*_*ude 21 javascript typedef extend jsdoc type-definition

假设您在ES6类(文档)中有以下代码:

/**
 * @typedef Test~options
 * @type {object.<string>}
 * @property {array} elements - An array containing elements
 * @property {number} length - The array length
 */

/**
 * @param  {Test~options} opt - Option object
 */
test(opt){

}
Run Code Online (Sandbox Code Playgroud)

现在我想记录另一个函数,让我们给它命名test2.此函数采用完全相同的options对象,但需要另一个属性parent.

如何记录这一点而不记录冗余选项?冗余意味着:

/**
 * @typedef Test~options
 * @type {object.<string>}
 * @property {array} elements - An array containing elements
 * @property {number} length - The array length
 */

/**
 * @param  {Test~options} opt - Option object
 */
test(opt){

}


/**
 * @typedef Test~options2
 * @type {object.<string>}
 * @property {array} elements - An array containing elements
 * @property {number} length - The array length
 * @property {object} parent - The parent element
 */

/**
 * @param  {Test~options2} opt - Option object
 */
 test2(opt){

 }
Run Code Online (Sandbox Code Playgroud)

Aji*_*mar 21

我找到了这个解决方案,这对我来说非常好。最初来自这里

 /**
 * @typedef {Object} ChildType
 * @property {String} childProp
 *
 * @typedef {Base & ChildType} Child
 */
Run Code Online (Sandbox Code Playgroud)

  • @Coderer:非常正确,但是由于 JSDoc 维护得不是很好(至少多年来没有扩展语言功能),我认为 JSDoc 的 TypeScript 风格确实是现在值得关注的*标准*,不仅是因为采用的原因,而且它是唯一支持通过“import()”自动发现第三方模块的候选者。FWIW,关于为什么我们计划将默认模式切换为打字稿模式的更多理由,请访问 https://github.com/gajus/eslint-plugin-jsdoc/issues/834,即使对于普通 JavaScript 用户(tsc 可以处理)也是如此。 (5认同)
  • 交集类型是 Typescript,并且在普通 JSDoc 中[不支持](https://github.com/jsdoc/jsdoc/issues/1285)。每个人都不断地将官方支持的 JSDoc 语法与 Typescript 的 JSDoc 解析实现混为一谈。我怀疑这是因为 Typescript 为 VSCode Intellisense 提供了支持,它拥有相当大的市场份额,即使在那些实际上没有编写 Typescript 的人中也是如此。 (4认同)
  • 它称为“[交集类型](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types)” (3认同)

lam*_*345 12

我不喜欢没有“完整设置”的半生不熟的答案,所以这里是一个:

/**
 * @typedef {Object} Person
 * @property {string} name - The person's name
 * @property {number} age - The person's age
 */
/**
 * @typedef {Object} WizardProperties
 * @property {string[]} magicPowers
 * @typedef {Person & WizardProperties} Wizard
 */
/** @type {Wizard} */
export const wizard = {
    name: "Harry",
    age: 20,
    magicPowers: ["brroooomm"]
}
Run Code Online (Sandbox Code Playgroud)

它也适用于多个文件,但您必须使用以下import('./person').Person样式:

  • 人.mjs
/**
 * @typedef {Object} Person
 * @property {string} name - The person's name
 * @property {number} age - The person's age
 */
Run Code Online (Sandbox Code Playgroud)
  • 向导.mjs
/**
 * @typedef {Object} WizardProperties
 * @property {string[]} magicPowers
 * @typedef {import('./person').Person & WizardProperties} Wizard
 */
/** @type {Wizard} */
export const wizard = {
  name: "Harry",
  age: 20,
  magicPowers: ["brroooomm", "wheeeeeeeeeeeew"]
}
Run Code Online (Sandbox Code Playgroud)


小智 7

试试吧

/**
 * @typedef {object.<string>} Test~options
 * @property {array} elements - An array containing elements
 * @property {number} length - The array length
 */

/**
 * @param {Test~options} opt - Option object
 */
test(opt){

}

/**
 * @typedef {Test~options} Test~options2
 * @property {object} parent - The parent element
 */

/**
 * @param  {Test~options2} opt - Option object
 */
test2(opt){

}
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止,这是最好的解决方案,因为它将“ Test〜options2”的“类型”指定为“ Test〜options”,并链接到后者。但这不是理想的,因为它没有重复“扩展”类型的所有参数。它仅列出新的参数。JSDoc应该为此进行更好的标记。 (2认同)