no-unsafe-assignment

Disallow assigning a value with type any to variables and properties.

Extending "plugin:@typescript-eslint/ recommended-type-checked " in an ESLint configuration enables this rule.

This rule requires type information to run.

The any type in TypeScript is a dangerous "escape hatch" from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code.

Despite your best intentions, the any type can sometimes leak into your codebase. Assigning an any typed value to a variable can be hard to pick up on, particularly if it leaks in from an external library.

This rule disallows assigning any to a variable, and assigning any[] to an array destructuring.

This rule also compares generic type argument types to ensure you don't pass an unsafe any in a generic position to a receiver that's expecting a specific type. For example, it will error if you assign Set<any> to a variable declared as Set<string> .

Try this rule in the playground ↗

  • ❌ Incorrect

There are cases where the rule allows assignment of any to unknown .

Example of any to unknown assignment that are allowed:

This rule is not configurable.

When Not To Use It ​

If your codebase has many existing any s or areas of unsafe code, it may be difficult to enable this rule. It may be easier to skip the no-unsafe-* rules pending increasing type safety in unsafe areas of your project. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

Related To ​

  • no-explicit-any
  • no-unsafe-argument
  • no-unsafe-call
  • no-unsafe-member-access
  • no-unsafe-return

Resources ​

  • Rule source
  • Test source
  • When Not To Use It

IMAGES

  1. jestjs

    typescript eslint unsafe assignment of an any value

  2. reactjs

    typescript eslint unsafe assignment of an any value

  3. TypeScript ESLint: Unsafe assignment of an any value [Fix]

    typescript eslint unsafe assignment of an any value

  4. Typescript ESLint errors with Jest functions (Unsafe call of an `any

    typescript eslint unsafe assignment of an any value

  5. ESLint: Unsafe call of an `any` typed value.(@typescript-eslint/no

    typescript eslint unsafe assignment of an any value

  6. typescript

    typescript eslint unsafe assignment of an any value

VIDEO

  1. Day 3: Tooling: Webpack, Babel, TypeScript ESLint, Prettier

  2. 👀 Time to ditch eslint? #tutorial #typescript #webdevelopment #javascript #programming #reactjs

  3. Use Destructuring Assignment to Pass an Object as a Function's Parameters (ES6) freeCodeCamp

  4. TypeScript vs ESLint #typescript #javascript

  5. typescript and Node Js Assignment Exercise no 3 I Governor IT Course

  6. Typescript exercise assignment 1,2 and 3 Governor Sindh IT course

COMMENTS

  1. javascript

    Why is TypeScript not having an issue with this code but the linter does? Do both not need to be alligned? In Typescript, something with type any can be assigned to anything. Using any essentially removes typesafety from that part of the code. For example: const foo: number = 'hello' as any // Typescript is fine with this

  2. no-unsafe-assignment

    no-unsafe-assignment. Disallow assigning a value with type any to variables and properties. Extending "plugin:@typescript-eslint/ recommended-type-checked " in an ESLint configuration enables this rule. This rule requires type information to run. The any type in TypeScript is a dangerous "escape hatch" from the type system.

  3. node.js

    Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.