42

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.

Tower
  • 98,741
  • 129
  • 357
  • 507

3 Answers3

78

I found the way to do it:

/**
 * @param {*} foo
 */
function bar(foo) {}
Tower
  • 98,741
  • 129
  • 357
  • 507
  • 2
    Where did you find that? My search results can only verify that this will work for the [Closure compiler](https://developers.google.com/closure/compiler/docs/js-for-compiler#types). Is it 'official' JSDoc syntax? – Robin van Baalen Jan 29 '13 at 15:55
  • @RobinvanBaalen Take a look at *The ALL type* the almost last: https://developers.google.com/closure/compiler/docs/js-for-compiler?hl=de#types – yckart Mar 20 '13 at 10:00
  • 4
    @yckart Like I said before; [JSDoc](http://usejsdoc.org/) != [Closure compiler](http://developers.google.com/closure). – Robin van Baalen Mar 20 '13 at 12:56
  • But gc is one of the major jsdoc-definers ;) Here's a good ressource about jsdoc and its *background*: http://wiki.servoy.com/display/public/DOCS/Annotating+JavaScript+using+JSDoc;jsessionid=5DD1388ED3DA0D59FC43114AC9492E0A#AnnotatingJavaScriptusingJSDoc-TypeExpressions – yckart Mar 20 '13 at 13:23
  • 2
    I created an [issue for adding this type](https://github.com/jsdoc3/jsdoc/issues/468) in the JSDoc 3 issue tracker. – Sebastian Zartner Aug 01 '13 at 21:16
  • 6
    This is in jsdoc documentation now: http://usejsdoc.org/tags-param.html#multiple-types-and-repeatable-parameters – sorohan Feb 07 '17 at 08:59
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
Dmitry
  • 470
  • 6
  • 9
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;
    }
Daniel Abyan
  • 303
  • 2
  • 6