开发者

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

So I have a helper namespace which I store helpful additions when developing JS. Now I plan to document them better and strengthen my JS with JsDoc and the help of Google Closure compiler. I got the lastest versions as of 2PM today. However I get errors with the when running the compiler on the following code:

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;
    }
}

So on both return lines I get the compiler error "ERROR - inconsistent return type"

How do I use ternary operators like this with the Google closure compiler? And yes I've Googled, but I just keep getting irrelevant search results. For now I will remove the ternary but it would prefer to use them without errors:

So I updated my sta开发者_JAVA百科tements as suggested by "Tomasz Nurkiewicz", but I'm still getting the errors: Changed made to code:

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";
    }
}

Compiler 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

Even when I try to set the type to {Boolean|null} I still get the errors. What gives?


You should declare your return type as {boolean} instead of {Boolean} because {boolean} refers to the primitive boolean type whereas {Boolean} refers to the wrapper {Boolean} type.


Will this help? In addition you have cleaner and more readable code...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜