Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc - Documenting a mixed array

In JSDoc, how to document an array accepting multiple object class like this:

var arr = [new Foo(), new Bar()];

How to write the type such that Foo and Bar are the only class that are accepted in the array?

like image 453
Kevin Law Avatar asked Mar 25 '16 17:03

Kevin Law


1 Answers

If the type of the first object should always be Foo and the second always Bar, we're looking at what would be understood as a tuple object. In this case, the correct JSDoc type would be:

/** @type {[Foo, Bar]} */
const arr = [new Foo(), new Bar()];

And not Array<Foo|Bar> as one could've thought, since the latter allows for things like [new Foo(), new Foo()], which is probably undesired.

like image 92
Lucio Paiva Avatar answered Sep 28 '22 06:09

Lucio Paiva