JsDoc - how to specify the length of an array

In jsDoc I can specify the parameters of the array and their members as follows:

 /** * @constructor * @param {Array.<string>} myArray */ function someFunction( myArray ){ this.firstArray = myArray; /** @member {Array.<float>} */ this.secondArray = []; } 

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

+6
source share
2 answers

I think you are asking if you can include a minimum / maximum length in type expressions (e.g. Array.<string> ).

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

+4
source

I looked at UseJSDoc.org and the Googles Closure Compiler , and no documentation describes how to specify the length of an array.

My assumption is that the compiler only checks the type and not the length, so even if there is a syntax for explicitly describing the length of the array, an array of the wrong length will probably pass the compiler anyway (no error will be selected).

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

 /** * Duplicates a 4-length array into itself. * eg `[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 .

If this feature is important to you (Id, of course, use it!), You can send a question .

+1
source

All Articles