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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With