[tslint]预期一个 'for-of' 循环而不是一个 'for' 循环与这个简单的迭代(prefer-for-of)

[tslint]Expected a 'for-of' loop instead of a 'for' loop with this simple iteration (prefer-for-of)

我的 for 循环出现 tslint 错误,当我尝试解决它时它说要转换为 for-of。我看过很多文档,但它不是 helpful.How 我可以解决 lint 错误而且我不能做 tslint:disable-next-line:prefer-for-of

for (let i = 0; i < this.rows.length; ++i) {
    if (!this.rows[i].selected) {
        this.selectAllChecked = false;
        break;
    }
}

要求您使用如下格式。 of 关键字遍历数组中的对象而不是遍历数组的索引。我假设它正在触发,因为您仅使用索引作为获取数组中值的方式(可以使用 of 语法清除)。

for (let row of this.rows) {
    if (!row.selected) {
        this.selectAllChecked = false;
        break;
    }
}

请注意,您可以使用以下 one-liner:

来完成同样的事情
this.selectAllChecked = this.rows.every(row => row.selected);

tslint.json里面我们可以添加规则:

"rules": {
    "prefer-for-of": false
}

这将通过禁用相关的 lint 规则来解决问题。