type/no-for-in-array
Disallow for-in iteration over array-like values.
| Default severity | Presets | Automatic fix | Options |
|---|---|---|---|
warning |
nuxt, opinionated |
No | None |
Applies to: Type information in Vue SFC scripts and templates, for the constructs shown below.
This rule reads type information from your TypeScript project (tsconfig.json), so the configuration sets typeAware: true. typeAware does not turn on opt-in rules by itself.
Bad
for-in walks the keys of an array instead of its element values.
<script setup lang="ts">
const numbers = [1, 2, 3];
function loopsKeys(): void {
for (const key in numbers) {
console.log(key);
}
}
</script>
Good
for-of walks the numeric elements of the array.
<script setup lang="ts">
const numbers = [1, 2, 3];
function loopsKeys(): void {
for (const n of numbers) {
console.log(n);
}
}
</script>
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"type/no-for-in-array": "warn"
},
"typeAware": true
},
});
Then run vp run lint.