如何让闭包编译器在调用函数时停止抱怨联合类型?
我在使用 jQuery-1.4.3 externs 文件时尤其看到这一点。 javadoc 上写着
/**
* @param {(string|number|function(number,number))=} arg1
* @return {(number|jQueryObject)}
* @nosideeffects
*/
jQueryObject.prototype.width = function(arg1) {};
我有一个如下所示的调用:
var w = $(window).width();
$('#whatever').width(w)
Closure 抱怨: 警告 - jQueryObject.prototype.height 的实际参数 1 与形式参数不匹配 找到:(jQueryObject|null|number) 必需:(函数(数字,数字):?|数字|字符串|未定义) $('#Xobni').height($(window).height());
通过尝试(删除可能的返回类型),我可以看到问题是第一次调用 width 可能返回一个 jQueryObject,并且由于这不是有效的输入,所以 Closure 给了我一个错误。我尝试添加这个:
/**
* @type {number}
*/
var w = $(window).width();
$('#Xobni').width(w);
但随后关闭抱怨: 警告 - 初始化变量 找到:(jQueryObject|null|number) 必填:数量 var w = $(窗口).width();
问题是当 width 接受参数时,它返回一个 jQueryObject。当它不带参数时,它返回一个数字。所以我知道我的调用没问题,但是 javadocs 并没有完全反映这一点,所以 Closure 警告我。有没有办法适当地修复 javadoc 或者有办法告诉 Closure 我知道这个结果将是一个数字。我知道我可能可以抑制错误,但我想知道如何更好地注释这些内容。
感谢您的帮助!
I'm seeing this in particular when using the jQuery-1.4.3 externs file. The javadocs for that reads
/**
* @param {(string|number|function(number,number))=} arg1
* @return {(number|jQueryObject)}
* @nosideeffects
*/
jQueryObject.prototype.width = function(arg1) {};
I have a call that looks like this:
var w = $(window).width();
$('#whatever').width(w)
Closure complains:
WARNING - actual parameter 1 of jQueryObject.prototype.height does not match formal parameter
found : (jQueryObject|null|number)
required: (function (number, number): ?|number|string|undefined)
$('#Xobni').height($(window).height());
From playing around (removing the possible return types), I can see that the problem is that the first call to width can return possibly a jQueryObject, and since that's not a valid input, Closure gives me an error. I tried adding this:
/**
* @type {number}
*/
var w = $(window).width();
$('#Xobni').width(w);
But then Closure complains:
WARNING - initializing variable
found : (jQueryObject|null|number)
required: number
var w = $(window).width();
The problem is that when width takes an argument, it returns a jQueryObject. When it doesn't take an argument, it returns a number. So I know my call is okay, but the javadocs don't quite reflect that, and so Closure is warning me. Is there a way to fix up the javadocs appropriately or a way to tell Closure I know this result will be a number. I know I can probably suppress the error, but I'd like to know how to annotate these things better.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以按如下方式覆盖它:
You can override this as follows:
试试这个:
或者这个:
Try this:
or this: