Avoid assigning a value with type any

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

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

const x = 1 as any;
const x = 1 as any,
function foo(a = 1 as any) {}
class Foo { constructor(private a = 1 as any) {} }
class Foo { private a = 1 as any; }
const [x] = 1 as any;
const [x] = [] as any[];

// TS treats the assignment pattern weirdly in this case
[[[[x]]]] = [1 as any];
const x = [...(1 as any)];
const x = [...([] as any[])];

const x = { y: 1 as any };
const x = { y: { z: 1 as any } };
const x = { ...(1 as any) };
<Foo a={1 as any} />;

Compliant Code Examples

const x = 1;
const x: number = 1;
const x = 1, y = 1;
let x;
let x = 1, y;
function foo(a = 1) {}
class Foo { constructor(private a = 1) {} }
class Foo { private a = 1; }
const x: Set<string> = new Set();
const x: Set<string> = new Set<string>();
const [x] = [1];
const [x, y] = [1, 2] as number[];
const [x, ...y] = [1, 2, 3, 4, 5];
const [x, ...y] = [1];
const [{ ...x }] = [{ x: 1 }] as [{ x: any }];
function foo(x = 1) {}
function foo([x] = [1]) {}
function foo([x, ...y] = [1, 2, 3, 4, 5]) {}
function foo([x, ...y] = [1]) {}
// this is not checked, because there's no annotation to compare it with
const x = new Set<any>();
const x = { y: 1 };
const x = { y = 1 };
const x = { y(){} };
const x: { y: number } = { y: 1 };
const x = [...[1, 2, 3]];
const [{ [`x${1}`]: x }] = [{ [`x`]: 1 }] as [{ [`x`]: any }];
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis