admin管理员组文章数量:1243256
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?
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?
-
Is this the correct way?
/** @type {[Foo|Bar]} */
– Kevin Law Commented Mar 25, 2016 at 17:27 -
3
I believe that you are looking for
{Array.<Foo|Bar>}
– Pandawan Commented Mar 25, 2016 at 17:33 -
2
Another option is
{(Foo|Bar)[]}
– austin_ce Commented May 16, 2019 at 15:34 -
@KevinLaw
[Foo|Bar]
would mean a one element tuple where the first/only element isFoo
orBar
. – dannymo Commented Dec 3, 2021 at 7:58
1 Answer
Reset to default 15If 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.
本文标签: javascriptJSDocDocumenting a mixed arrayStack Overflow
版权声明:本文标题:javascript - JSDoc - Documenting a mixed array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740106922a2225475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论