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

236
Vistas
What is the correct way to document a optional parameter of type boolean in JSDOC and WebStorm?

I've reviewed a few different Stack Overflow answers that got me to this point. So far I have tried the following.

    /**
     * Prints true, false or empty string
     * @param {boolean=} b
     */
    test = (b = '') => { // Initialized type string not assignable to variable type boolean
        console.log(`b = "${b}"`)
    }
    /**
     * Prints true, false or empty string
     * @param {boolean=} b
     */
    test = (b) => {
        // Initialized type string not assignable to variable type boolean
        if (typeof b !== 'boolean') b = ''
        console.log(`b = "${b}"`)
    }

I have a feeling the correct answer is suppress the warning, but hoping someone else has a better answer.

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

0

In your code, the type can be either string or boolean, so you need using type union here:

/**
     * Prints true, false or empty string
     * @param {(boolean|string)=} b
     */
    test = (b) => {
        // Initialized type string not assignable to variable type boolean
        if (typeof b !== 'boolean') b = ''
        console.log(`b = "${b}"`)
    }

See https://jsdoc.app/tags-type.html

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