package.json中eslint-config-react-app配置无效,overrides属性类型错误
错误原因
ESLint的overrides配置项规范要求必须是数组格式,每个数组元素对应一套针对特定文件的覆盖规则,你当前的配置直接写了单个对象,不符合类型要求所以校验失败。
修正方法
找到项目package.json里的eslintConfig字段,把里面的overrides属性对应的值,改成包裹了原有对象的数组即可。
错误配置示例(package.json片段)
{ "eslintConfig": { "extends": "react-app", "overrides": { "files": ["**/*.ts","**/*.tsx"], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": 2018, "sourceType": "module", "ecmaFeatures": {"jsx":true}, "warnOnUnsupportedTypeScriptVersion": true }, "plugins": ["@typescript-eslint"], "rules": { "default-case":"off", "no-dupe-class-members":"off", "@typescript-eslint/no-angle-bracket-type-assertion":"warn", "no-array-constructor":"off", "@typescript-eslint/no-array-constructor":"warn", "@typescript-eslint/no-namespace":"error", "no-unused-vars":"off", "@typescript-eslint/no-unused-vars":["warn",{"args":"none","ignoreRestSiblings":true}], "no-useless-constructor":"off", "@typescript-eslint/no-useless-constructor":"warn" } } } }
修正后的正确配置
{ "eslintConfig": { "extends": "react-app", "overrides": [ { "files": ["**/*.ts","**/*.tsx"], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": 2018, "sourceType": "module", "ecmaFeatures": {"jsx":true}, "warnOnUnsupportedTypeScriptVersion": true }, "plugins": ["@typescript-eslint"], "rules": { "default-case":"off", "no-dupe-class-members":"off", "@typescript-eslint/no-angle-bracket-type-assertion":"warn", "no-array-constructor":"off", "@typescript-eslint/no-array-constructor":"warn", "@typescript-eslint/no-namespace":"error", "no-unused-vars":"off", "@typescript-eslint/no-unused-vars":["warn",{"args":"none","ignoreRestSiblings":true}], "no-useless-constructor":"off", "@typescript-eslint/no-useless-constructor":"warn" } } ] } }
修改完成后重启ESLint服务即可生效,如果仍有报错可以删除node_modules文件夹和包管理锁文件(package-lock.json/yarn.lock/pnpm-lock.yaml)后重新安装依赖。
内容的提问来源于stack exchange,提问作者CodeWithMe
相关产品推荐
相关产品推荐

