带三元组的 Google 闭包编译器:错误 - 返回类型不一致

发布于 2024-11-19 03:18:55 字数 1562 浏览 5 评论 0原文

所以我有一个辅助命名空间,我在开发 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";
    }
}

编译器输出:

[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} 时,我仍然收到错误。什么给?

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 statements 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-11-26 03:18:55

您应该将返回类型声明为 {boolean} 而不是 {Boolean},因为 {boolean} 引用原始布尔类型,而 {Boolean} 指的是包装器 {Boolean} 类型。

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.

找回味觉 2024-11-26 03:18:55

这会有帮助吗?此外,您还有更清晰、更易读的代码......

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

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";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文