noDuplicateObjectKeys
诊断类别:lint/suspicious/noDuplicateObjectKeys
¥Diagnostic Category: lint/suspicious/noDuplicateObjectKeys
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
与以下相同:
no-dupe-keys
¥Same as:
no-dupe-keys
禁止在对象内部使用两个同名的键。
¥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
code-block.js:2:5 lint/suspicious/noDuplicateObjectKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This property value named a is later overwritten by an object member with the same name.
1 │ const obj = {
> 2 │ a: 1,
│ ^^^^
3 │ a: 2,
4 │ }
ℹ Overwritten with this value.
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 value named a
1 1 │ const obj = {
2 │ - ···→ a:·1,
3 2 │ a: 2,
4 3 │ }
code-block.js:2:5 lint/suspicious/noDuplicateObjectKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This setter named a 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 value.
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 named a
1 1 │ const obj = {
2 │ - ···→ set·a(v)·{},
3 2 │ a: 2,
4 3 │ }
¥Valid
¥Related links
自从:v1.0.0
¥Since: v1.0.0
禁止在对象内部使用两个同名的键。
¥Disallow two keys with the same name inside objects.
¥Examples
¥Invalid
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 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
¥Related links