【发布时间】:2019-10-01 05:37:22
【问题描述】:
我对我的 JS 代码使用 google-closure 编译器和 jsLint 工具。因为闭包编译器查看 JSDoc 标签,所以我需要将变量转换为正确的类型,否则编译器会抛出错误。下面的代码可以正常工作(没有编译器警告),但是当我运行 jsLint 时出现“奇怪的分配”错误。有没有其他方法可以转换变量。
/** @return {Town|Village|Park|Metropolis} */
var getCurrentItem = function() {...some code}
var item = getCurrentItem();
if (condition)
{
item = /** @type {Town} */ (item); // 'Weird assignment' error occurs
drawTown(item);
updateTown(item)
}
else
{
item = /** @type {Village} */ (item); // 'Weird assignment' error occurs
drawVillage(item);
updateVillage(item)
}
我希望在一行中完成转换,而不是在我需要调用的每个函数上完成!
【问题讨论】:
-
查看github.com/jamesallardice/jslint-error-explanations/blob/master/… 听起来您需要一种方法来记录
item的类型,而无需重新分配它 -
你在使用
gjslint吗?现在已弃用:github.com/google/closure-linter -
@CertainPerformance 你是对的。我知道在 javascript 中为自身分配变量没有意义,但我需要它来进行转换。不幸的是,您的链接仅适用于 window.location 或其他浏览器全局变量。
-
@johnlinp 不。我没有使用 gjsLint。
-
@royalBlue 我明白了。
标签: javascript google-closure-compiler jsdoc jslint jshint