Reputation: 230008
function foo(a, opt_b) {
opt_b = opt_b || 1;
...
}
foo(1); // IntelliJ will yell at me, saying "Invalid number of parameters, expected 2"
Is there a way to document foo()
such that IntelliJ won't yell at me?
Upvotes: 13
Views: 2922
Reputation: 140
According to http://usejsdoc.org/tags-param.html there are two ways to use comments to declare a parameter as optional:
/**
* @param {string} [somebody] - Somebody's name.
*/
function sayHello(somebody) {
if (!somebody) {
somebody = 'John Doe';
}
alert('Hello ' + somebody);
}
and
/**
* @param {string=} somebody - Somebody's name.
*/
function sayHello(somebody) {
if (!somebody) {
somebody = 'John Doe';
}
alert('Hello ' + somebody);
}
Both ways worked for me.
Upvotes: 2
Reputation: 276
Using JavaDoc-style comments for JavaScript, you can declare a parameter as optional with brackets [] around it:
/**
* My function
* @param [optionalParam] An optional parameter
*/
myFunction: function(optionalParam) {
//...
},
Upvotes: 25
Reputation: 710
"JavaScript > General > Signature Mismatch Problem" is the inspection setting that will disable the incorrect-number-of-parameters warning for you. Unfortunately, it affects more than just parameter count. According to the description inside IntelliJ IDEA, it "Checks JavaScript called function parameters, return values, assigned expressions to be of correct type. The validation works in JavaScript, html or jsp files."
Upvotes: 7