// My function does X and Y.
// @params {object} parameters An object containing the parameters
// @params {function} callback The callback function
function(parameters, callback) {
}
But how do I describe how the parameters object should be structured? For example it should be something like:
{
setting1 : 123, // (required, integer)
setting2 : 'asdf' // (optional, string)
}
I see that there is already an answer about the @return tag, but I want to give more details about it.
First of all, the official JSDoc 3 documentation doesn't give us any examples about the @return for a custom object. Please see http://usejsdoc.org/tags-returns.html. Now, let's see what we can do until some standard will appear.
Function returns object where keys are dynamically generated. Example:
{1: 'Pete', 2: 'Mary', 3: 'John'}
. Usually, we iterate over this object with the help offor(var key in obj){...}
.Possible JSDoc according to https://google.github.io/styleguide/javascriptguide.xml#JsTypes
Function returns object where keys are known constants. Example:
{id: 1, title: 'Hello world', type: 'LEARN', children: {...}}
. We can easily access properties of this object:object.id
.Possible JSDoc according to https://groups.google.com/forum/#!topic/jsdoc-users/TMvUedK9tC4
Fake It.
The Full Monty.
Define a type.
According to https://google.github.io/styleguide/javascriptguide.xml#JsTypes
The record type.
From the @param wiki page:
Parameters With Properties
If a parameter is expected to have a particular property, you can document that immediately after the @param tag for that parameter, like so:
There used to be a @config tag which immediately followed the corresponding @param, but it appears to have been deprecated (example here).
For
@return
tag use{{field1: Number, field2: String}}
, see: http://wiki.servoy.com/display/public/DOCS/Annotating+JavaScript+using+JSDocThere's a new
@config
tag for these cases. They link to the preceding@param
.By now there are 4 different ways to document objects as parameters/types. Each has its own uses. Only 3 of them can be used to document return values, though.
For objects with a known set of properties (Variant A)
This syntax is ideal for objects that are used only as parameters for this function and don't require further description of each property. It can be used for
@returns
as well.For objects with a known set of properties (Variant B)
Very useful is the parameters with properties syntax:
This syntax is ideal for objects that are used only as parameters for this function and that require further description of each property. This can not be used for
@returns
.For objects that will be used at more than one point in source
In this case a @typedef comes in very handy. You can define the type at one point in your source and use it as a type for
@param
or@returns
or other JSDoc tags that can make use of a type.You can then use this in a
@param
tag:For objects whose values are all the same type
The first type (string) documents the type of the keys which in JavaScript is always a string or at least will always be coerced to a string. The second type (number) is the type of the value; this can be any type. This syntax can be used for
@returns
as well.Resources
Useful information about documenting types can be found here:
http://usejsdoc.org/tags-type.html
PS:
to document an optional value you can use []:
or: