配置@typescript-eslint/no-explicit-any报meta.hasSuggestions错误
问题背景
- 在项目
.eslintrc.json中添加"@typescript-eslint/no-explicit-any": "error"规则,希望禁止代码中使用any类型,配置添加后项目编译失败。 - 完整ESLint配置如下:
{ "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" }
- 已尝试的排查操作:
- 已知ESLint 8+存在同类规则校验问题,但当前项目使用ESLint版本为7.32.0,不符合该问题的版本前提
- 尝试手动在配置中添加
meta: {hasSuggestions: true}字段,未解决问题 - 检索公开技术问答内容未找到适配当前依赖版本的解决方案
问题根因
报错和本地ESLint主版本号没有直接关联,核心原因是依赖版本不兼容:
你安装的eslint-plugin-prettier@4.0.0版本内置了ESLint 8的规则校验逻辑,会强制要求所有提供代码修复建议的规则在元信息中显式声明meta.hasSuggestions: true。但当前使用的@typescript-eslint/eslint-plugin@4.33.0是适配ESLint 7的版本,该版本的规则统一通过meta.docs.suggestion标记建议功能,没有新增meta.hasSuggestions字段,校验不通过就会抛出对应错误。
手动在用户配置文件中添加meta字段属于无效操作,规则的元信息由插件本身导出时定义,用户侧的ESLint配置没有权限覆盖修改插件内置规则的元属性。
解决方案
两个方案二选一即可,优先选择第一种,改动成本最低:
方案1:降级eslint-plugin-prettier到ESLint7兼容版本
执行对应包管理工具的安装命令,安装3.x最终稳定版:
# npm用户 npm install eslint-plugin-prettier@3.4.1 --save-dev # yarn用户 yarn add eslint-plugin-prettier@3.4.1 -D
3.x版本的eslint-plugin-prettier完全适配ESLint 7的规则校验逻辑,不会触发meta.hasSuggestions强制校验,安装完成后重启ESLint服务、重新执行编译即可让no-explicit-any规则正常生效。
方案2:全量升级ESLint生态依赖到ESLint8适配版本
如果需要使用高版本prettier插件的特性,可以将ESLint相关依赖统一升级到适配版本:
- 升级
eslint到8.x正式版本 - 升级
@typescript-eslint/eslint-plugin、@typescript-eslint/parser、@typescript-eslint/typescript-estree到5.x以上版本 - 同步升级
eslint-config-airbnb-typescript到适配ESLint 8的对应版本
升级完成后重新安装依赖、重启编译进程即可恢复正常。
内容的提问来源于stack exchange,提问作者Borking Bamboozler
相关产品推荐
相关产品推荐

