如何让闭包编译器在调用函数时停止抱怨联合类型?

发布于 2024-09-29 20:31:56 字数 1045 浏览 0 评论 0原文

我在使用 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 技术交流群。

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

发布评论

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

评论(2

凉城凉梦凉人心 2024-10-06 20:31:56

您可以按如下方式覆盖它:

var w = /** @type {number} */ ($(window).width());
$('#whatever').width(w)

You can override this as follows:

var w = /** @type {number} */ ($(window).width());
$('#whatever').width(w)
饭团 2024-10-06 20:31:56

试试这个:

var w =  $(window).width();
w = parseInt(w.toString());
$('#whatever').width(w);

或者这个:

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