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.
How to JsDoc a "mixed" type?
Asked Answered
I found the way to do it:
/**
* @param {*} foo
*/
function bar(foo) {}
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
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
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;
}
© 2022 - 2024 — McMap. All rights reserved.