【问题标题】:What is the correct way to document a optional parameter of type boolean in JSDOC and WebStorm?在 JSDOC 和 WebStorm 中记录布尔类型的可选参数的正确方法是什么?
【发布时间】:2021-10-05 21:36:11
【问题描述】:

我已经查看了一些不同的 Stack Overflow 答案,这些答案让我走到了这一步。到目前为止,我已经尝试了以下方法。

    /**
     * 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}"`)
    }

我感觉正确的答案是压制警告,但希望其他人有更好的答案。

【问题讨论】:

    标签: javascript intellij-idea webstorm jsdoc


    【解决方案1】:

    在您的代码中,类型可以是stringboolean,因此您需要在此处使用类型联合:

    /**
         * 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}"`)
        }
    

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

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 1970-01-01
      • 2014-08-04
      • 2023-03-14
      • 2013-08-25
      • 2010-11-11
      • 2017-02-06
      • 1970-01-01
      相关资源
      最近更新 更多