Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!