【问题标题】:JsDoc: How do I document an object onlyJsDoc:我如何只记录一个对象
【发布时间】:2021-12-31 08:24:44
【问题描述】:

在 JSDoc 中,如何实际记录一个对象,而不是作为一个参数?

例子

let object1 = {
  name: '' //string,
  age: '' //number,
};

真实例子

 /**
   * Update User
   * @param {Object} params
   * @param {String} params.oldName
   * @param {String} params.oldAge
   */
  const onChangeUser = params => {
    ....

    //I want to document this object
    let object1 = {
      // name: '', string,
      // age: '' number,
    };
  };

这个对象不属于任何函数,只是函数内部的一个对象,但不用作参数

【问题讨论】:

标签: javascript jsdoc


【解决方案1】:

可以使用 typedef @documentation 为复杂类型添加别名

/**
 * @typedef PersonProperties
 * @type {object}
 * @property {string} name - Full Name of the Person.
 * @property {number} age  - Age of Person.
 */

用法:

/** @type {PersonProperties} */
let object1 = {
  name: '',
  age: ''
};

【讨论】:

  • 谢谢!!...但如果不想添加单独的'typedef',因为可能对象并没有那么大,无法将它实际分离为 typedef,有没有办法将其用作“内联”? :)
  • 我还没有测试过内联的 google 闭包编译器语法,你能检查一下这是否适用于/** @type {{name: string, age: number}} */ 更多关于 google 闭包编译器类型转换的内容github.com/google/closure-compiler/wiki/…
  • 出于可读性考虑,我会将typedefs 记录到顶部,用法几乎是单行的。
猜你喜欢
  • 2015-11-24
  • 2019-04-06
  • 2017-11-20
  • 2021-05-04
  • 2016-07-18
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多