How to JsDoc a “mixed” type?

馋奶兔 提交于 2019-12-21 03:10:53

问题


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 myself missing one and making it overly complex. I tried using the Mixed keyword, but it popups errors in many IDEs such as WebStorm.


回答1:


I found the way to do it:

/**
 * @param {*} foo
 */
function bar(foo) {}



回答2:


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



回答3:


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;
    }


来源:https://stackoverflow.com/questions/4909259/how-to-jsdoc-a-mixed-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!