简体   繁体   English

@typescript-eslint/no-unsafe-* 规则的新 eslint 错误

[英]New eslint errors with the @typescript-eslint/no-unsafe-* rules

I'm having some trouble with adding them into some existing projects.我在将它们添加到一些现有项目中时遇到了一些麻烦。 For example, I have a class in a module that I developed:例如,我在我开发的模块中有一个 class:

export default class ClassName {
  // Class members
}

Now I import that into another project:现在我将其导入另一个项目:

import ClassName from 'modulename';

const object = new ClassName();

I get 2 errors on this line.我在这条线上收到 2 个错误。

On the object in const object :const object object的 object 上:

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

On the new in new ClassName :newnew ClassName中:

error  Unsafe construction of an any type value         @typescript-eslint/no-unsafe-call

How can I avoid these errors?!我怎样才能避免这些错误?! I would really like to be able to follow these rules because I think they'd be so useful!我真的很想能够遵守这些规则,因为我认为它们会非常有用!

Thanks.谢谢。


Here's another example:这是另一个例子:

import { readJsonSync } from 'fs-extra';
const testEnv = readJsonSync(testEnvPath);

Here I get the no-unsafe-assignment error on the testEnv of const testEnv , and the no-unsafe-call error on the readJsonSync call on the second line.在这里,我在const testEnvtestEnv上得到了no-unsafe-assignment错误,在第二行的readJsonSync调用中得到了no-unsafe-call错误。

I can get rid of the first one with this code:我可以用这段代码摆脱第一个:

interface ITestEnv {
  // interface members
}
const testEnv: ITestEnv = readJsonSync(testEnvPath) as ITestEnv;

however, I still can't figure out how to get rid of the second one on the readJsonSync call.但是,我仍然无法弄清楚如何摆脱readJsonSync调用中的第二个。

ESlint can not resolve the absolute import of your module, Eslint 无法解析你的模块的绝对导入,
which makes it to infer your class type as any .这使得它可以将您的 class 类型推断为any

Make sure baseUrl and paths in tsconfig.json file used by ESlint are defined correctly.确保正确定义了 ESlint 使用的tsconfig.json文件中的baseUrlpaths

@see Typescript – Module Resolution @see Typescript – 模块分辨率

In the first case, you have just one error coming from the constructor, which is cascading to the const assignment.在第一种情况下,您只有一个错误来自构造函数,它级联到 const 赋值。 Something in your class implementation is making the type-inference to be inferred as any .您的 class 实现中的某些内容正在将类型推断推断为any

Not saying your code is incorrect.不是说你的代码不正确。 It might be, but there's an ( open issue on Github ) reporting a constructor as being incorrectly flagged by the same rule.可能是,但有一个( Github 上的未解决问题)报告构造函数被同一规则错误标记。

On your second issue, have you added @type/fs-extra as a project dependency?关于你的第二个问题,你是否添加了@type/fs-extra作为项目依赖? Many npm packages do not have types themselves.许多 npm 包本身没有类型。 Types are created by someone and added to the @types library .类型由某人创建并添加到@types 库中。 When that's the case, the @types/package_name must be added as a dependency separately.在这种情况下, @types/package_name必须单独添加为依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM