I am writing a JavaScript module and I would like to write a good JSDoc for it. I have a multiple variables defined within single var (var x = 1, y = functionA;
etc.). One of those variables holds a function reference (may have only 3 choices).
Now my question: is there a way to document it like above, so that my IDE (WebStorm) also recognize the variable type (atm. I get a warning when assigning other function reference to this variable)?
Try using inline doc comments for this, like:
var /**Number*/ num = 1, /**String*/ str = "";
This is the correct JSDoc syntax which will work just fine in Webstorm for auto-completion and type checking:
let
/** @type {Number} */ someNumber,
/** @type {String} */ someString;
Try setting a string to someNumber
and see Webstorm complaining about it:
For functions, you can also specify its parameters and its return value. For instance:
/** @type {function(Number, String): Number[]} */
Is a function that receives a number as first param, a string as second and returns an array of numbers.
Note: although it works pretty well for auto-completion, currently Webstorm fails to do type checking for functions. For instance, it allows me to do this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With