簡體   English   中英

帶有三元組的 Google 閉包編譯器:錯誤 - 不一致的返回類型

[英]Google closure compiler w/ ternaries: ERROR - inconsistent return type

所以我有一個輔助命名空間,我在開發 JS 時存儲有用的添加。 現在我計划更好地記錄它們並使用 JsDoc 和 Google Closure 編譯器的幫助來增強我的 JS。 我得到了截至今天下午 2 點的最新版本。 但是,在以下代碼上運行編譯器時出現錯誤:

var my.company.tool = {
    "isNumber": function( p_value )
    {
            return ( typeof(p_value) == "number" ) ? true : false;
    },
    /**
    * @static
    * @returns {Boolean} Indicative of an object.
    */
    "isObject": function( p_value )
    {
            return ( typeof(p_value) == "object" ) ? true : false;
    }
}

所以在兩條返回線上我得到編譯器錯誤“錯誤 - 不一致的返回類型”

如何在 Google 閉包編譯器中使用像這樣的三元運算符? 是的,我用谷歌搜索過,但我總是得到不相關的搜索結果。 現在我將刪除三元,但它更願意在沒有錯誤的情況下使用它們:

所以我按照“Tomasz Nurkiewicz”的建議更新了我的陳述,但我仍然收到錯誤:更改為代碼:

var my.company.tool = {
    "isNumber": function( p_value )
    {
            return typeof(p_value) == "number";
    },
    /**
    * @static
    * @returns {Boolean} Indicative of an object.
    */
    "isObject": function( p_value )
    {
            return typeof(p_value) == "object";
    }
}

編譯器 output:

[pakeException]
    js/core/IHR.js:68: ERROR - inconsistent return type
    found   : boolean
    required: (Boolean|null)
            return typeof( p_value ) == "number";
                                     ^

    js/core/IHR.js:76: ERROR - inconsistent return type
    found   : boolean
    required: (Boolean|null)
            return ( typeof( p_value ) == "object" );
                                       ^

    2 error(s), 0 warning(s), 99.0% typed

即使我嘗試將類型設置為 {Boolean|null} ,我仍然會收到錯誤消息。 是什么賦予了?

您應該將返回類型聲明為{boolean}而不是{Boolean}因為{boolean}指的是原始 boolean 類型,而{Boolean}指的是包裝器{Boolean}類型。

這會有幫助嗎? 此外,您還有更清晰、更易讀的代碼......

var my.company.tool = {
    "isNumber": function( p_value )
    {
            return typeof(p_value) == "number";
    },
    "isObject": function( p_value )
    {
            return typeof(p_value) == "object";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM