如何在非TypeScript的React项目中禁用ESLint类型错误?
React非TS项目ESLint类型错误解决思路
1. 清理ESLint配置里的TS相关内容
打开你的ESLint配置文件(.eslintrc.js/.eslintrc.json等):
- 删除
plugins数组里的@typescript-eslint - 移除
extends字段中plugin:@typescript-eslint/recommended这类TS相关的扩展配置 - 删除
rules里所有带@typescript-eslint/前缀的规则,或直接将它们设为off
2. 卸载TS相关的ESLint依赖
从你的package.json能看到安装了TS版ESLint插件,直接执行卸载命令:
npm uninstall @typescript-eslint/eslint-plugin @typescript-eslint/parser # 使用yarn的话执行 yarn remove @typescript-eslint/eslint-plugin @typescript-eslint/parser
3. 针对性禁用类型错误规则(若上述步骤未解决)
如果还有残留的类型错误,直接在ESLint配置的rules里关闭对应规则:
{ "rules": { "@typescript-eslint/no-unsafe-assignment": "off", "@typescript-eslint/no-unsafe-member-access": "off", "@typescript-eslint/no-unsafe-call": "off", // 其他出现的类型错误规则都按此格式设为off } }
4. 清理冗余的TS依赖
如果项目完全不使用TypeScript,把package.json里的typescript依赖也卸载:
npm uninstall typescript # 或执行 yarn remove typescript
5. 重启编辑器和ESLint服务
修改完配置后,重启VS Code(或你使用的编辑器),再重启ESLint服务(VS Code中按Ctrl+Shift+P,搜索ESLint: Restart ESLint Server执行)
内容的提问来源于stack exchange,提问作者Samet Sunman
相关产品推荐
相关产品推荐

