我正在尝试向 .eslintrc.json 添加规则以禁止
any
类型。
我将规则 (
"@typescript-eslint/no-explicit-any": "error"
) 添加到我的设置中:
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"airbnb-typescript",
"prettier"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module",
"project": ["./tsconfig.json", "cypress/tsconfig.json"]
},
"plugins": ["react", "react-hooks", "@typescript-eslint", "prettier"],
"rules": {
"react/prop-types": "off",
"react/react-in-jsx-scope": "off",
"react/require-default-props": "off",
"import/prefer-default-export": "off",
"import/no-relative-parent-imports": "error",
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object"
],
"alphabetize": {
"order": "asc"
},
"newlines-between": "always"
}
],
"prettier/prettier": "error",
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/semi": "warn",
"@typescript-eslint/no-explicit-any": "error",
"max-len": [
2,
{
"code": 150,
"ignorePattern": "^import .*"
}
]
}
}
但我收到错误:
Failed to compile.
Rules with suggestions must set the `meta.hasSuggestions` property to `true`. `meta.docs.suggestion` is ignored by ESLint.
Occurred while lining <name of file>
Rule: "@typescript-eslint/no-explicit-any"
这些是一些使用过的软件包及其版本:
"@types/react": "^17.0.45",
"@types/react-datepicker": "^4.4.1",
"@types/react-dom": "^17.0.17",
"@types/react-router-dom": "^5.3.3",
"@types/styled-components": "^5.1.25",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"@typescript-eslint/typescript-estree": "^4.33.0",
"axe-core": "^4.4.2",
"cypress": "^9.6.1",
"cypress-axe": "^0.14.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.30.0",
"eslint-plugin-react-hooks": "^4.5.0",
"jest-junit": "^13.2.0",
"prettier": "^2.6.2",
"prettier-eslint": "^13.0.0"
我在 StackOverflow 上看到另一篇文章建议使用 eslint-plugin-react-hooks 来解决另一个错误,但找不到与我尝试应用的规则类似的内容。另外,有些人提到了 eslint 8+ 版本的问题,但根据我拥有的软件包,我的版本似乎是:7.32.0
添加
meta: {hasSuggestions: true}
不起作用,并给我带来与 这张票 中相同的错误
任何帮助将不胜感激。
更新到所有
*eslint*
软件包的最新版本 - 特别是 eslint
和 @typescript-eslint/*
- 错误应该会消失。请务必运行包管理器的安装命令,例如 npm i
。
说明:“必须设置
meta.hasSuggestions
属性” 错误已在 ESLint 8 中添加。在该要求之前编写的一些 ESLint 插件的旧版本并不总能满足它。因此,如果您使用较新版本的 ESLint 和较旧的插件,您可能会收到该错误。
一般来说,尝试始终使用相对较新版本的软件包。特别是当您看到其中神秘的错误时。这个问题来自 2022 年,但 package.json
提到了版本 4 的 typescript-eslint 软件包。typescript-eslint@5 于 2021 年发布。