Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Unsafe Assignment of An Any Value ESLint Error

Fairly new to Typescript and have this small block of code:

let payload;
if (event.body) {
  payload = JSON.parse(event.body);
}

The payload line is throwing the below error from eslint:

Unsafe assignment of an any value. eslint(@typescript-eslint/no-unsafe-assignment

This is more of an exercise in understanding the intricacies with Typescript and eslint working together, what can I do to resolve this issue? Seems like a minor change would do the trick but I haven't been able to locate a preexisting question with the same intent. Thanks in advance for the help!

like image 375
Franchise Avatar asked Sep 03 '20 15:09

Franchise


2 Answers

I got it.

let payload: unknown;

Tests pass for eslint as a result.

like image 198
Franchise Avatar answered Nov 20 '22 06:11

Franchise


Found solution on the https://github.com/typescript-eslint/typescript-eslint/issues/2118

We can use external function to get data with set type of return like:

function(str: string): Superobj {
  return JSON.parse() as Superobj
}
like image 2
Alex Avatar answered Nov 20 '22 07:11

Alex