如何在jsdoc中描述“对象”类型的参数?

478
// My function does X and Y.
// @params {object} parameters An object containing the parameters
// @params {function} callback The callback function
function(parameters, callback) {
}

但是我该如何描述参数对象应该如何构造呢?例如,它应该类似于:

{
  setting1 : 123, // (required, integer)
  setting2 : 'asdf' // (optional, string)
}

开始使用TypeScript吧,尽可能避免使用JavaScript。 - Md. A. Apu
6个回答

588

@param页面


具有属性的参数

如果预期参数具有特定的属性,您可以在该参数的@param标记后立即记录该属性,如下所示:

 /**
  * @param userInfo Information about the user.
  * @param userInfo.name The name of the user.
  * @param userInfo.email The email of the user.
  */
 function logIn(userInfo) {
        doLogIn(userInfo.name, userInfo.email);
 }

曾经有一个@config标签,紧跟在相应的@param后面,但它似乎已被弃用(示例在此)。


20
很遗憾,“returns”标签似乎在http://code.google.com/p/jsdoc-toolkit/wiki/TagReturns中没有对应项。 - Michael Bylstra
2
在这个类似的答案中 https://dev59.com/Jmgv5IYBdhLWcg3wVPTU#14820610 ,他们也在开头添加了 @param {Object} options。不过我猜这可能是多余的。 - pcatre
2
有没有想法如何文档化一个可选的对象成员?我的用户对象应该有用户名,并且可以有全名。那么我该如何指定全名是可选的呢? - Yash Kumar Verma

417
到目前为止,有四种不同的方法来记录对象作为参数/类型。每种方法都有自己的用途。但是只有其中三种可以用于记录返回值。 对于具有已知属性集的对象(变体A)
/**
 * @param {{a: number, b: string, c}} myObj description
 */

这种语法适用于仅作为此函数参数使用且不需要每个属性的进一步描述的对象。它也可用于 @returns
对于已知属性集的对象(变体B),带有属性的参数语法非常有用。
/**
 * @param {Object} myObj description
 * @param {number} myObj.a description
 * @param {string} myObj.b description
 * @param {} myObj.c description
 */

此语法适用于仅用作此函数参数并需要进一步描述每个属性的对象。但此语法不能用于@returns
对于将在源代码中多次使用的对象,@typedef非常方便。您可以在源代码的一个点定义类型,并将其用作@param@returns或其他可使用类型的JSDoc标签的类型。
/**
 * @typedef {Object} Person
 * @property {string} name how the person is called
 * @property {number} age how many years the person lived
 */

然后您可以在@param标签中使用这个:

/**
 * @param {Person} p - Description of p
 */

或者在@returns中:

/**
 * @returns {Person} Description
 */

对于值都是相同类型的对象

/**
 * @param {Object.<string, number>} dict
 */

第一种类型(字符串)记录了键的类型,在JavaScript中始终为字符串或至少将强制转换为字符串。第二种类型(数字)是值的类型;这可以是任何类型。 这种语法也可以用于@returns
资源
有关记录类型的有用信息可以在此处找到:

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

提示:

要记录一个可选值,您可以使用[]

/**
 * @param {number} [opt_number] this number is optional
 */

或者:

/**
 * @param {number|undefined} opt_number this number is optional
 */

1
变量1是否适用于属性的多种类型?例如 {{dir: A|B|C }} - CMCDragonkai
1
对于那些键是动态生成的对象呢?比如{[myVariable]: string} - Frondor
vscode(版本1.51以下)无法很好地处理@typedef,它不能显示用于提示的对象属性,因此我目前坚持使用param {Object}。 - Qiulang
1
现在,VSCode对于@typedef方法的使用已经可以正常工作了。 - JackMorrissey
1
这在VSCode中是有效的并且可以运行。 - Harikrushna Patel
显示剩余4条评论

152
我看到已经有一个关于@return标记的答案,但我想提供更多详细信息。
首先,官方的JSDoc 3文档没有为自定义对象提供任何关于@return的示例。请参见https://jsdoc.app/tags-returns.html。现在,让我们看看在出现某些标准之前我们能做些什么。
  • Function returns object where keys are dynamically generated. Example: {1: 'Pete', 2: 'Mary', 3: 'John'}. Usually, we iterate over this object with the help of for(var key in obj){...}.

    Possible JSDoc according to https://google.github.io/styleguide/javascriptguide.xml#JsTypes

    /**
     * @return {Object.<number, string>}
     */
    function getTmpObject() {
        var result = {}
        for (var i = 10; i >= 0; i--) {
            result[i * 3] = 'someValue' + i;
        }
        return result
    }
    
  • Function returns object where keys are known constants. Example: {id: 1, title: 'Hello world', type: 'LEARN', children: {...}}. We can easily access properties of this object: object.id.

    Possible JSDoc according to https://groups.google.com/forum/#!topic/jsdoc-users/TMvUedK9tC4

    • Fake It.

      /**
       * Generate a point.
       *
       * @returns {Object} point - The point generated by the factory.
       * @returns {number} point.x - The x coordinate.
       * @returns {number} point.y - The y coordinate.
       */
      var pointFactory = function (x, y) {
          return {
              x:x,
              y:y
          }
      }
      
    • The Full Monty.

      /**
       @class generatedPoint
       @private
       @type {Object}
       @property {number} x The x coordinate.
       @property {number} y The y coordinate.
       */
      function generatedPoint(x, y) {
          return {
              x:x,
              y:y
          };
      }
      
      /**
       * Generate a point.
       *
       * @returns {generatedPoint} The point generated by the factory.
       */
      
      var pointFactory = function (x, y) {
          return new generatedPoint(x, y);
      }
      
    • Define a type.

      /**
       @typedef generatedPoint
       @type {Object}
       @property {number} x The x coordinate.
       @property {number} y The y coordinate.
       */
      
      
      /**
       * Generate a point.
       *
       * @returns {generatedPoint} The point generated by the factory.
       */
      
      var pointFactory = function (x, y) {
          return {
              x:x,
              y:y
          }
      }
      

    According to https://google.github.io/styleguide/javascriptguide.xml#JsTypes

    • The record type.

      /**
       * @return {{myNum: number, myObject}}
       * An anonymous type with the given type members.
       */
      function getTmpObject() {
          return {
              myNum: 2,
              myObject: 0 || undefined || {}
          }
      }
      

22

5
原始链接没有任何有用的地方。我认为它的新版本在这里:https://wiki.servoy.com/display/Serv7/Annotating+JavaScript+Using+JSDoc - John Krull

18

如果期望一个参数具有特定的属性,您可以通过提供额外的@param标签来记录该属性。例如,如果预期一个名为employee的参数具有名称和部门属性,则可以按以下方式进行记录:

```html

如果期望一个参数具有特定的属性,您可以通过提供额外的@param标签来记录该属性。例如,如果预期一个名为employee的参数具有名称和部门属性,则可以按以下方式进行记录:

```
/**
 * Assign the project to a list of employees.
 * @param {Object[]} employees - The employees who are responsible for the project.
 * @param {string} employees[].name - The name of an employee.
 * @param {string} employees[].department - The employee's department.
 */
function(employees) {
    // ...
}

如果一个参数被解构而没有明确的名称,你可以给对象一个合适的名称并记录它的属性。

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function({ name, department }) {
    // ...
};

来源:JSDoc


0

对于这些情况,现在有一个新的@config标签。它们链接到前面的@param

/** My function does X and Y.
    @params {object} parameters An object containing the parameters
    @config {integer} setting1 A required setting.
    @config {string} [setting2] An optional setting.
    @params {MyClass~FuncCallback} callback The callback function
*/
function(parameters, callback) {
    // ...
};

/**
 * This callback is displayed as part of the MyClass class.
 * @callback MyClass~FuncCallback
 * @param {number} responseCode
 * @param {string} responseMessage
 */

1
您能为@config标签指向文档吗? 我在 usejsdoc.org 上没有找到任何信息,并且此页面表明@config已经被弃用。 - Dan Dascalescu
5
我认为@config在当前已经被弃用,YUIDoc推荐使用@attribute代替。 - Mike DeSimone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接