Correct way to document open-ended argument functions in JSDoc

后端 未结 4 1415
粉色の甜心
粉色の甜心 2020-11-29 00:49

Let\'s say you have something like the following:

var someFunc = function() {
    // do something here with arguments
}

How would you corre

4条回答
  •  情话喂你
    2020-11-29 01:43

    How to do this is now described in the JSDoc documentation, and it uses an ellipsis like the Closure docs do.

    @param {...}  
    

    You need to supply a type to go after the ellipsis, but you can use a * to describe accepting anything, or use the | to separate multiple acceptable types. In the generated documentation JSDoc will describe this argument as repeatable, in the same way it describes optional arguments as optional.

    In my testing there was no need to have an argument in the actual javascript function definition, so your actual code can just have empty parentheses, i.e. function whatever() { ... }.

    Single type:

    @param {...number} terms Terms to multiply together
    

    Any type (in the example below, the square brackets mean items will get tagged as both optional and repeatable):

    @param {...*} [items] - zero or more items to log.
    

    Multiple types need parentheses around the type list, with the ellipsis before the opening paren:

    @param {...(Person|string)} attendees - Meeting attendees, listed as either 
                                            String names or {@link Person} objects
    

提交回复
热议问题