Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:typescript-best-practices/no-dupe-keys
Language: TypeScript
Severity: Error
Category: Error Prone
Description
Object literals should not have duplicate keys. If you define an object with duplicate keys, the last one will overwrite any preceding ones.
Non-Compliant Code Examples
varx={a: b,['a']:b};varx={y: 1,y: 2};varx={'':1,'':2};varx={'':1,[``]:2};varfoo={0x1: 1,1: 2};// should be captured by no-octal
// var x = { 012: 1, 10: 2 };
varx={0b1: 1,1: 2};varx={0o1: 1,1: 2};varx={1n: 1,1: 2};varx={1_0: 1,10: 2};varx={"z":1,z: 2};varfoo={bar: 1,bar: 1,bar() {}}varx={a: 1,get['a'](){}};varx={a: 1,seta(value){}};varx={a: 1,b:{a: 2},getb() {}};varx=({'/(?<zero>0)/':1,[/(?<zero>0)/]:2})// while we can't evaluate the value of the template string we will assume that
// two keys with the exact same template string will evaluate to the same key
constobj={props:{[`${classes.foo}${classes.bar}`]:():boolean=>{returnnull;},[`${classes.foo}${classes.bar}`]:():boolean=>{returnnull;},}}
Compliant Code Examples
varfoo={__proto__: 1,two: 2};varx={foo: 1,bar: 2};varx={'':1,bar: 2};varx={'':1,' ':2};varx={'':1,[null]:2};varx={'':1,[a]:2};varx={[a]:1,[a]:2};varx={a: b,[a]:b};varx={a: b,...c}varx={a: 1,b:{a: 2}};varx=({null:1,[/(?<zero>0)/]:2})var{a,a}=obj// should be captured by no-octal
// var x = { 012: 1, 12: 2 };
varx={1_0: 1,1: 2};// template literals should be valid as long as they are not exactly the same
constobj={props:{[`${classes.foo}${classes.bar}`]:():boolean=>{returnnull;},[`${classes.baz}${classes.bla}`]:():boolean=>{returnnull;},}}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- typescript-best-practices # Rules to enforce TypeScript best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines