Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
JS Doc , is there any way to specify "Choice" param?

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda