admin管理员组文章数量:1289344
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 plains: 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 plains: 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!
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 plains: 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 plains: 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!
Share Improve this question edited Oct 28, 2010 at 0:56 Mr. Ubin asked Oct 28, 2010 at 0:38 Mr. UbinMr. Ubin 1432 silver badges6 bronze badges2 Answers
Reset to default 8You can override this as follows:
var w = /** @type {number} */ ($(window).width());
$('#whatever').width(w)
Try this:
var w = $(window).width();
w = parseInt(w.toString());
$('#whatever').width(w);
or this:
var w = $(window).width() + '';
$('#whatever').width(w);
本文标签:
版权声明:本文标题:javascript - How do I get the Closure Compiler to stop complaining about union types when calling a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741395421a2376349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论