noDuplicateObjectKeys
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noDuplicateObjectKeys¥Diagnostic Category:
lint/suspicious/noDuplicateObjectKeys -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
¥How to configure
{ "linter": { "rules": { "suspicious": { "noDuplicateObjectKeys": "error" } } }}¥Description
禁止在对象内部使用两个同名的键。
¥Disallow two keys with the same name inside objects.
¥Examples
¥Invalid
{ "title": "New title", "title": "Second title"}code-block.json:2:3 lint/suspicious/noDuplicateObjectKeys ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The key title was already declared.
1 │ {
> 2 │ “title”: “New title”,
│ ^^^^^^^
3 │ “title”: “Second title”
4 │ }
ℹ This is where a duplicated key was declared again.
1 │ {
2 │ “title”: “New title”,
> 3 │ “title”: “Second title”
│ ^^^^^^^
4 │ }
5 │
ℹ If a key is defined multiple times, only the last definition takes effect. Previous definitions are ignored.
¥Valid
{ "title": "New title", "secondTitle": "Second title"}¥Related links
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noDuplicateObjectKeys¥Diagnostic Category:
lint/suspicious/noDuplicateObjectKeys -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-dupe-keys相同¥Same as
no-dupe-keys
-
¥How to configure
{ "linter": { "rules": { "suspicious": { "noDuplicateObjectKeys": "error" } } }}¥Description
禁止在对象内部使用两个同名的键。
¥Disallow two keys with the same name inside objects.
如果多次定义同名的对象属性(将 getter 与 setter 组合时除外),则只有最后一个定义会进入对象,而先前的定义将被忽略,这可能是一个错误。
¥If an object property with the same name is defined multiple times (except when combining a getter with a setter), only the last definition makes it into the object and previous definitions are ignored, which is likely a mistake.
¥Examples
¥Invalid
const obj = { a: 1, a: 2,}code-block.js:2:5 lint/suspicious/noDuplicateObjectKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This property is later overwritten by an object member with the same name.
1 │ const obj = {
> 2 │ a: 1,
│ ^^^^
3 │ a: 2,
4 │ }
ℹ Overwritten with this property.
1 │ const obj = {
2 │ a: 1,
> 3 │ a: 2,
│ ^^^^
4 │ }
5 │
ℹ If an object property with the same name is defined multiple times (except when combining a getter with a setter), only the last definition makes it into the object and previous definitions are ignored.
ℹ Unsafe fix: Remove this property.
1 1 │ const obj = {
2 │ - ···→ a:·1,
3 2 │ a: 2,
4 3 │ }
const obj = { set a(v) {}, a: 2,}code-block.js:2:5 lint/suspicious/noDuplicateObjectKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This setter is later overwritten by an object member with the same name.
1 │ const obj = {
> 2 │ set a(v) {},
│ ^^^^^^^^^^^
3 │ a: 2,
4 │ }
ℹ Overwritten with this property.
1 │ const obj = {
2 │ set a(v) {},
> 3 │ a: 2,
│ ^^^^
4 │ }
5 │
ℹ If an object property with the same name is defined multiple times (except when combining a getter with a setter), only the last definition makes it into the object and previous definitions are ignored.
ℹ Unsafe fix: Remove this setter.
1 1 │ const obj = {
2 │ - ···→ set·a(v)·{},
3 2 │ a: 2,
4 3 │ }
¥Valid
const obj = { a: 1, b: 2,}const obj = { get a() { return 1; }, set a(v) {},}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号