带三元组的 Google 闭包编译器:错误 - 返回类型不一致
所以我有一个辅助命名空间,我在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将返回类型声明为
{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.这会有帮助吗?此外,您还有更清晰、更易读的代码......
Will this help? In addition you have cleaner and more readable code...