The any type in TypeScript is dangerously broad, leading to unexpected behavior. Using any should be avoided.
Non-Compliant Code Examples
constx=1asany;constx=1asany,functionfoo(a=1asany){}classFoo{constructor(privatea=1asany){}}classFoo{privatea=1asany;}const[x]=1asany;const[x]=[]asany[];// TS treats the assignment pattern weirdly in this case
[[[[x]]]]=[1asany];constx=[...(1asany)];constx=[...([]asany[])];constx={y: 1asany};constx={y:{z: 1asany}};constx={...(1asany)};<Fooa={1asany}/>;
Compliant Code Examples
constx=1;constx: number=1;constx=1,y=1;letx;letx=1,y;functionfoo(a=1){}classFoo{constructor(privatea=1){}}classFoo{privatea=1;}constx: Set<string>=newSet();constx: Set<string>=newSet<string>();const[x]=[1];const[x,y]=[1,2]asnumber[];const[x,...y]=[1,2,3,4,5];const[x,...y]=[1];const[{...x}]=[{x: 1}]as[{x: any}];functionfoo(x=1){}functionfoo([x]=[1]){}functionfoo([x,...y]=[1,2,3,4,5]){}functionfoo([x,...y]=[1]){}// this is not checked, because there's no annotation to compare it with
constx=newSet<any>();constx={y: 1};constx={y=1};constx={y(){}};constx:{y: number}={y: 1};constx=[...[1,2,3]];const[{[`x${1}`]:x}]=[{[`x`]:1}]as[{[`x`]:any}];
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