google-closure-compiler - 将 JSON 对象注释为 Google Closure Compiler 的数组
问题描述
我需要将 JSON 对象注释为数组,但我无法让它工作:
/** @type {Array} */
let resp = JSON.parse(response);
for (let item of resp) {
}
闭包编译器返回:
WARNING - initializing variable found : * required: (Array|null)
let resp = JSON.parse(response);
^^^^^^^^^^^^^^^^^^^^
解决方案
因为 JSON 解析几乎可以返回任何内容,所以您必须对结果进行类型转换:
let resp = /** @type {Array} */ (JSON.parse(response));
注意额外的括号
您可以考虑在数组中添加项目的类型:
/** @type {Array<string>} */
推荐阅读
- html - 当我使用它应该是什么时,我的 HTML favicon 不工作
- mime-types - 将 PDF 数据转换为八位字节流以进行打印机自动检测
- javascript - 使用 EJS 文件进入另一个页面
- kotlin - Kotlin 代表出错了
- math - Lua:如何计算50到500之间的随机数,平均结果为100?
- android - CardView 中突出显示的文本没有颜色
- android - 类型 androidx.viewpager.widget.PagerAdapter 被定义多次
- javascript - 在 React 17 中使用设置样式组件背景图像的道具
- javascript - 如何检查 HTMLCollection 中的所有元素是否包含某个属性的某个值?
- laravel - 如何在 laravel 运行时设置或更改全局变量值