I'm pretty new to JS and JS Doc and I had a question for optional parameters.
Imagine this function
/**
* @param {string} [param1]
* @param {string} [param2]
*/
const foo= (){...}
As JSDoc says, an optional parameter is like this
/**
* @param {string} [somebody] - Somebody's name.
*/
Is there any way, with my example, to define 2 optional parameters but at least one is required ?(If param1 is set, param2 is not and if param2 is set, param1 is not).
Thanks for help.
The square brackets around the parameter name means it's optional. To make it required, remove the squared brackets.
/**
* @param {string} param1 - required
* @param {string} [param2] - optional
*/
const foo= (){...}
You could also just use Typescript, for a more robust type system.
const foo = (param1: string, param2?: string);
Is there any way, with my example, to define 2 optional parameters but at least one is required ?(If param1 is set, param2 is not and if param2 is set, param1 is not).
To solve this problem, you can define multiple function overloads.
function foo(email:string);
function foo(username:string){
if(username) return "Used username overlaod";
if(email) return "Used email overload";
}
For JSDoc it's harder. Looks like this:
/**
*
* @constructor
* @param {string} username
*
* @constructor
* @param {string} email
*/
function foo(value) {
// ...
}
I would recommend using TS over JSDoc.