ESLint无法找到plugin:@typescript-eslint/recommended-type-checked配置求助
解决TypeScript项目中ESLint
no-floating-promises规则的配置问题 问题根源
- 直接写
no-floating-promises规则是错误的:这个规则属于@typescript-eslint插件,必须加前缀写成@typescript-eslint/no-floating-promises,原生ESLint没有该规则,所以会提示找不到定义。 - 找不到
plugin:@typescript-eslint/recommended-type-checked配置:要么是@typescript-eslint依赖版本过低(该配置在v5.50.0+版本才引入),要么是依赖安装不完整。
分步解决
1. 修正规则名称
把.eslintrc.json中的规则项修改:
原来的:
"no-floating-promises": "warn"
改为:
"@typescript-eslint/no-floating-promises": "warn"
2. 升级依赖版本
查看package.json里的@typescript-eslint/eslint-plugin和@typescript-eslint/parser版本,若低于v5.50.0,执行升级命令:
npm用户:
npm install @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest --save-dev
yarn用户:
yarn add @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest --dev
3. 修复parserOptions配置
JSON配置文件不支持__dirname变量,将tsconfigRootDir改为项目根目录的相对路径./,完整配置如下:
"parserOptions": { "ecmaVersion": "latest", "sourceType": "module", "tsconfigRootDir": "./", "project": ["./tsconfig.json"] }
注意:确保tsconfig.json在项目根目录存在,路径无误。
4. 最终配置示例
修正后的.eslintrc.json核心部分参考:
{ "env": { "es2021": true, "node": true, "es6": true }, "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended-type-checked" ], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": "latest", "sourceType": "module", "tsconfigRootDir": "./", "project": ["./tsconfig.json"] }, "plugins": ["@typescript-eslint"], "rules": { "@typescript-eslint/no-floating-promises": "warn" } }
5. 重新运行lint
执行lint命令即可,例如:
npx eslint . --ext .ts
内容的提问来源于stack exchange,提问作者Praxis
相关产品推荐
相关产品推荐

