我已經回顧了一些不同的 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}"`)
}
我有一種感覺,正確的答案是抑制警告,但希望其他人有更好的答案。
uj5u.com熱心網友回復:
在您的代碼中,型別可以是stringor boolean,因此您需要在此處使用型別聯合:
/**
* 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/319045.html
標籤:javascript 智能理念 网络风暴 jsdoc
