简体   繁体   English

任何值 ESLint 错误的 Typescript 不安全分配

[英]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/2118https://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.

相关问题 @typescript-eslint/no-unsafe-assignment:任何值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an any value ESLint:“any”的不安全分配和“any”类型值的不安全调用 - ESLint: Unsafe assignment of an `any` and Unsafe call of an `any` typed value Typescript:ESLint:任何类型值的不安全返回 @typescript-eslint/no-unsafe-return - Typescript : ESLint : Unsafe return of an any typed value @typescript-eslint/no-unsafe-return Typescript Jest 函数的 ESLint 错误(“any”类型值的不安全调用) - Typescript ESLint errors with Jest functions (Unsafe call of an `any` typed value) Typescript - eslint 错误分配给类型的“任何”类型的不安全参数 - Typescript - eslint error Unsafe argument of type 'any' assigned to a type 使用 Firebase config.ts 文件设置 Vue 3 + Quasar 时,我收到此 eslint 错误:Unsafe assignment of an `any` value - When setting up Vue 3 + Quasar with a Firebase config.ts file I get this eslint error: Unsafe assignment of an `any` value 任何值上的 eslint 错误不安全成员访问 ['content-type'] - eslint error Unsafe member access ['content-type'] on an any value Redux typescript-eslint/no-unsafe-assignment - useDispatch - Redux typescript-eslint/no-unsafe-assignment - useDispatch Typescript React 输入 onchange 不安全赋值 'any' - Typescript React input onchange unsafe assignment 'any' VSCode Eslint 错误 no-unsafe-assignment 显示不正确 - VSCode Eslint Error no-unsafe-assignment Incorrectly Displayed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM