Ce produit n'est pas pris en charge par le site Datadog que vous avez sélectionné. ().
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-unsafe-assignment
Language: TypeScript
Severity: Warning
Category: Error Prone
Description
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}];
1
2
rulesets:- typescript-best-practices # Rules to enforce TypeScript best practices.