[英]Typescript Unsafe Assignment of An Any Value ESLint Error
Fairly new to Typescript and have this small block of code:相当新的 Typescript 并且有这个小代码块:
let payload;
if (event.body) {
payload = JSON.parse(event.body);
}
The payload line is throwing the below error from eslint:有效载荷行从 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?这更像是一种理解 Typescript 和 eslint 协同工作的复杂性的练习,我该怎么做才能解决这个问题? 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!
在此先感谢您的帮助!
I got it.我知道了。
let payload: unknown;
Tests pass for eslint as a result.结果,eslint 的测试通过了。
Found solution on the https://github.com/typescript-eslint/typescript-eslint/issues/2118在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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.