How to JsDoc a “mixed” type?

前端 未结 3 854
时光说笑
时光说笑 2021-02-11 14:46

Simple question, how do I document that \"Mixed-type\"? I know I could just list all possible types like {null|undefined|String|Number|Object} and end up finding my

相关标签:
3条回答
  • 2021-02-11 15:31

    Use {}

    There is an example from http://usejsdoc.org/tags-type.html:

    An object called 'myObj' with properties 'a' (a number), 'b' (a string) and 'c' (any type).

    {{a: number, b: string, c}} myObj
    // or:
    {Object} myObj
    {number} myObj.a
    {string} myObj.b
    {} myObj.c
    
    0 讨论(0)
  • 2021-02-11 15:37

    I found the way to do it:

    /**
     * @param {*} foo
     */
    function bar(foo) {}
    
    0 讨论(0)
  • 2021-02-11 15:53

    In JSDoc, you can describe values in different ways. For example, using the following tags @type, @param, @return. You can specify optional values using the "?". Here is an example

        /**
         * Returns string or null
         *
         * @param {?string} nullableStringArgument
         *
         * @return {?string}
         */
        function returnNullableString (nullableStringArgument = null) {
            /** @type {?string} */
            const nullableString = [null, 'string'][Math.floor(Math.random() * 2)];
    
            return nullableString;
        }
    
    0 讨论(0)
提交回复
热议问题