Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unsafe assignment of an error typed value" typescript-eslint error

At the time of asking this question, "Unsafe assignment of an error typed value" returned zero Google results, so it seems worth an SO question.

The error is produced by linting the following TypeScript code

const map = {
  0.1: 'bar',
};

const x = Math.random();
// "ESLint: Unsafe assignment of an error typed value.(@typescript-eslint/no-unsafe-assignment)"
// https://typescript-eslint.io/rules/no-unsafe-assignment/
const s: string = map[x];
console.log(s);

eslintrc consists of this rule:

{
  "rules": {
    "@typescript-eslint/no-unsafe-assignment": "error"
  }
}

Here's a typescript-eslint playground showing the error.

What causes the linter to output this error?

Unsafe assignment of an error typed value. 8:7 - 8:25

What exactly is the "error" typed value it refers to?

One solution was to explicitly type map as const map: Record<number, string> but I'd like to understand that "error" typed value, because s could at most be assigned undefined.

like image 526
Dan Dascalescu Avatar asked Sep 02 '25 05:09

Dan Dascalescu


1 Answers

If you're getting this error in VSCode, try restarting the ESLint server (cmd-shift-P "restart eslint"). It doesn't always pick up new imports properly.

like image 178
Nils Avatar answered Sep 05 '25 01:09

Nils