How to JsDoc a "mixed" type?
Asked Answered
E

3

43

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.

Elastin answered 5/2, 2011 at 19:47 Comment(0)
E
81

I found the way to do it:

/**
 * @param {*} foo
 */
function bar(foo) {}
Elastin answered 21/7, 2011 at 15:32 Comment(6)
Where did you find that? My search results can only verify that this will work for the Closure compiler. Is it 'official' JSDoc syntax?Fordo
@RobinvanBaalen Take a look at The ALL type the almost last: developers.google.com/closure/compiler/docs/…Elwell
@Elwell Like I said before; JSDoc != Closure compiler.Fordo
But gc is one of the major jsdoc-definers ;) Here's a good ressource about jsdoc and its background: wiki.servoy.com/display/public/DOCS/…Elwell
I created an issue for adding this type in the JSDoc 3 issue tracker.Jeremyjerez
This is in jsdoc documentation now: usejsdoc.org/…Anecdotic
P
3

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
Poacher answered 15/9, 2017 at 13:21 Comment(0)
P
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;
    }
Playboy answered 1/11, 2019 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.