jsDoc - how to specify array length

a 夏天 提交于 2019-12-21 09:27:06

问题


In jsDoc I can specify my array parameters and members like this:

/**
 * @constructor
 * @param {Array.<string>} myArray
 */
function someFunction( myArray ){

    this.firstArray = myArray;

    /** @member {Array.<float>} */
    this.secondArray = [];

}

Is there also a way to specify the length, or minLength and maxLength of these arrays?


回答1:


I think you're asking whether you can include the minimum/maximum length in the type expressions (for example, Array.<string>).

In short, the answer is no. You'll need to document the minimum/maximum length in the description of each array.




回答2:


I have looked through UseJSDoc.org and Google’s Closure Compiler, and neither documentation describes how to specify array length.

My guess is the compiler only checks for type, not for length, so even if there were syntax to explicitly describe array length, an array of the incorrect length would probably still pass the compiler (no error would be thrown).

The best way to do this is in the human-language description of the parameter and return type:

/**
 * Duplicates a 4-length array into itself.
 * e.g. `[2, 3, 5, 8] => [2, 2, 3, 3, 5, 5, 8, 8]`
 * @param   {Array<number>} arr the array to duplicate (exactly 4 entries)
 * @returns {Array<number>} the result (an array of length 8)
 */
function dupe(arr) {
  ...
}

FYI, you can use Array.<number>, or Array<number>, or even number[] inside the @type declaration.

If this feature is important to you (I’d certainly use it!), you can submit an issue.



来源:https://stackoverflow.com/questions/22786245/jsdoc-how-to-specify-array-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!