How to JsDoc a "mixed" type?
17,086
Solution 1
I found the way to do it:
/**
* @param {*} foo
*/
function bar(foo) {}
Solution 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
Solution 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;
}
Author by
Tower
Updated on June 06, 2022Comments
-
Tower almost 2 years
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. -
Robin van Baalen about 11 yearsWhere did you find that? My search results can only verify that this will work for the Closure compiler. Is it 'official' JSDoc syntax?
-
yckart about 11 years@RobinvanBaalen Take a look at The ALL type the almost last: developers.google.com/closure/compiler/docs/…
-
Robin van Baalen about 11 years@yckart Like I said before; JSDoc != Closure compiler.
-
yckart about 11 yearsBut gc is one of the major jsdoc-definers ;) Here's a good ressource about jsdoc and its background: wiki.servoy.com/display/public/DOCS/…
-
Sebastian Zartner over 10 yearsI created an issue for adding this type in the JSDoc 3 issue tracker.
-
sorohan about 7 yearsThis is in jsdoc documentation now: usejsdoc.org/…