이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
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;},}}// a plain property paired with an unrelated sibling whose name happens
// to start with "set"/"get" is not a duplicate key
constuseStore=create((set)=>({loading: false,setloading:(valor)=>set({loading: valor}),}))
원활한 통합. Datadog Code Security 사용해 보기
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드 분석해 보기
이 규칙의 사용법
1
2
rulesets:- typescript-best-practices # Rules to enforce TypeScript best practices.
리포지토리 루트에서 위의 콘텐츠를 사용해 static-analysis.datadog.yml 생성
Datadog의 무료 IDE Plugins를 사용하거나 Code Security 스캔을 CI 파이프라인에 추가