简体   繁体   English

jsDoc - 如何指定数组长度

[英]jsDoc - how to specify array length

In jsDoc I can specify my array parameters and members like this:jsDoc ,我可以像这样指定我的数组参数和成员:

/**
 * @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?还有一种方法可以指定这些数组的lengthminLengthmaxLength吗?

I think you're asking whether you can include the minimum/maximum length in the type expressions (for example, Array.<string> ). 我想你是否可以在类型表达式中包含最小/最大长度(例如, Array.<string> )。

In short, the answer is no. 简而言之,答案是否定的。 You'll need to document the minimum/maximum length in the description of each array. 您需要记录每个数组描述中的最小/最大长度。

I have looked through UseJSDoc.org and Google's Closure Compiler , and neither documentation describes how to specify array length. 我查看了UseJSDoc.org和Google的Closure Compiler ,两个文档都没有描述如何指定数组长度。

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. 仅供参考,你可以在@type声明中使用Array.<number> ,或Array<number> ,甚至是number[]

If this feature is important to you (I'd certainly use it!), you can submit an issue . 如果此功能对您很重要(我当然会使用它!),您可以提交问题

While specifying the size [limits] of an Array is not possible, for fixed-length Arrays, like 'Tuples', a type expression can be used:虽然指定数组的大小 [limits] 是不可能的,但对于固定长度的数组,如 'Tuples',可以使用类型表达式

/**
 * @returns {[number, number, number]}
 */
function getLuckyThree() { … }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM