Skip to content
Advertisement

Unsafe use of expression of type ‘any’ for return statements in Typescript function

JavaScript

I keep getting a lint issue for this line .then((response): object => response.data)

which states Unsafe use of expression of type ‘any’

Advertisement

Answer

I suspect that it’s because response is a “generic object” and typescript can’t “identify” that it has a .data attribute.

In order to fix that we can declare an interface of a type:

JavaScript

and then use it to “explain” to TS that we expect the response to contain that attribute:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement