[英]Eslint Typescript "No Implicit Any" rule
I am setting up a new typescript project with eslint.我正在使用 eslint 设置一个新的 typescript 项目。 I am trying to setup eslint rules correctly so that the
tsc
command runs without errors.我正在尝试正确设置 eslint 规则,以便
tsc
命令运行时不会出错。 I have the problem with the "noImplicitAny" rule, which I use in tsconfig.json
, but I am unable check for this in eslint.我在 tsconfig.json 中使用的“noImplicitAny”规则有问题,但我无法在
tsconfig.json
中对此进行检查。
.eslintrc.js: .eslintrc.js:
module.exports = {
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json"],
sourceType: "module",
},
rules: {
"no-undef": "warn",
},
plugins: ["@typescript-eslint"],
settings: {
"import/resolver": {
node: {
extensions: [".js", ".ts"],
},
},
},
};
tsconfig.json: tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "ES6",
"declaration": true,
"outDir": "./lib",
"strict": true,
"noImplicitAny": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/__tests__/*"]
}
In short I want the eslint to check for the implicit any and warn about its usage.简而言之,我希望 eslint 检查隐式的 any 并警告其使用。 How can I configure the rules in
.eslintrc.js
to achieve this?我如何配置
.eslintrc.js
中的规则来实现这一点?
There are a number of no-unsafe-*
rules that have been implemented in TypeScript-ESLint:在 TypeScript-ESLint 中已经实现了许多
no-unsafe-*
规则:
no-unsafe-call
- forbids calling an expression typed as any
no-unsafe-call
- 禁止调用any
类型的表达式no-unsafe-member-access
- forbids using any
as a member name no-unsafe-member-access
- 禁止使用any
作为成员名称no-unsafe-argument
- forbids passing any
as a function parameter no-unsafe-argument
- 禁止将any
作为函数参数传递no-unsafe-assignment
- forbids using any
in an assignment no-unsafe-assignment
- 禁止在赋值中使用any
On a similar note, there's no-explicit-any
.同样,
no-explicit-any
。 Put all of these together, and you should be fully protected against any
problems.把所有这些放在一起,你应该得到充分保护,免受
any
问题的影响。
eslint refuses to add a no-implicit-any due to the fact it duplicates a typescript rule. eslint 拒绝添加 no-implicit-any,因为它复制了 typescript 规则。
They did however make a transition rule that should not be used permanently which does what you wanted.然而,他们确实制定了一个不应永久使用的过渡规则,它可以满足您的要求。 You should remove this rule once you can turn these options on in your tsconfig file.
一旦可以在 tsconfig 文件中打开这些选项,就应该删除此规则。
This allows warning for implicit parameters or arguments in normal and arrow functions.这允许对普通函数和箭头函数中的隐式参数或 arguments 发出警告。 There are more options than the ones commented out, documentation linked below.
有比注释掉的选项更多的选项,下面链接的文档。
'@typescript-eslint/typedef': [
'warn',
{
parameter: true,
arrowParameter: true,
// variableDeclaration: true,
// memberVariableDeclaration: true,
// objectDestructuring: true,
},
],
link to github issue not allowing no-implicit-any https://github.com/typescript-eslint/typescript-eslint/issues/3979链接到 github 问题不允许 no-implicit-any https://github.com/typescript-eslint/typescript-eslint/issues/3979
doucmentation for typedef information https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/typedef.md typedef 信息的文档 https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/typedef.md
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.