typescript/prefer-for-of Style
它的作用
强制使用 for-of 循环,而不是用于简单迭代的 for 循环。
为什么这不好?
在数组上使用用于简单迭代的 for 循环,可以替换为更简洁、 更易读的 for-of 循环。for-of 循环更容易阅读,也更不容易出错,因为它们 消除了对索引变量和手动数组访问的需要。
示例
此规则的错误代码示例:
typescript
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}此规则的正确代码示例:
typescript
for (const item of arr) {
console.log(item);
}如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/prefer-for-of": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/prefer-for-of": "error",
},
});bash
oxlint --deny typescript/prefer-for-of版本
此规则于 v0.2.16 中添加。
