ITADN

Rule proposal: no-array-index-key

#3031OpenGrawl 创建于 2026-02-23
G
Grawlcommented
## Please describe what the rule should do: Disallow using array item index as key in `v-for` iteration. This rule should do the same as [no-array-index-key in eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md) ## What category should the rule belong to? - [ ] Enforces code style (layout) - [x] Warns about a potential error (problem) - [ ] Suggests an alternate way of doing something (suggestion) - [ ] Other (please specify:) ## Code examples that this rule should warn about Incorrect code: ```vue <template> <ul> <!-- List is an array, item index cannot be a stable key --> <li v-for="(item, index) in list" :key="index"></li> </ul> </template> ``` Correct code: ```vue <template> <ul> <!-- List is an array, each item has unique stable key named id --> <li v-for="item in list" :key="item.id"></li> </ul> </template> ``` Correct code: ```vue <template> <ul> <!-- List is an object, each key is unique stable key --> <li v-for="(value, key) in object" :key="key"></li> </ul> ``` ## Additional context This issue is a duplicate of #1746 from 2021. I hope now we can determine a difference between array and object in `v-for` at a linter level using TypeScript.
0 条评论