将 JSON 对象注释为 Google 闭包编译器的数组

Annotate JSON object as array for Google Closure Compiler

我需要将 JSON 对象注释为数组,但我无法让它工作:

/** @type {Array} */
let resp = JSON.parse(response);

for (let item of resp) {

}

闭包编译器returns:

WARNING - initializing variable found   : * required: (Array|null)
        let resp = JSON.parse(response); 
               ^^^^^^^^^^^^^^^^^^^^

因为 JSON 解析可以 return 几乎任何东西,你必须对结果进行类型转换:

let resp = /** @type {Array} */ (JSON.parse(response));

注意额外的括号

您可以考虑在数组中添加项目类型:

/** @type {Array<string>} */