我环顾四周,找不到任何与此完全匹配的内容,并且没有任何修复程序可以为我解决这个问题。我对 eslint 比较陌生,确实需要一些帮助。
我的目标是为我的 TypeScript 项目启用规则
"no-floating-promises"
。当我添加它并运行 lint 时,我得到了数百个实例:
error Definition for rule 'no-floating-promises' was not found no-floating-promises
。根据我读到的内容,这是因为我需要扩展:
"plugin:@typescript-eslint/recommended-type-checked"
因此,我更新了我的
.eslintrc.json
以将 "plugin:@typescript-eslint/recommended-type-checked"
添加到扩展中。
现在,当我运行 lint 时,我得到:
Oops! Something went wrong! :(
ESLint: 8.45.0
ESLint couldn't find the config "plugin:@typescript-eslint/recommended-type-checked" to extend from. Please check that the name of the config is correct.
The config "plugin:@typescript-eslint/recommended-type-checked" was referenced from the config file in "/Users/[username]/gitdir/[project]/.eslintrc.json".
If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.
我正在努力找出解决此问题的后续步骤。任何方向将不胜感激!
我的 .eslintrc.json 包括:
{
"env": {
"es2021": true,
"node": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-type-checked"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest"
},
"plugins": ["@typescript-eslint"],
...
"no-floating-promises": "warn"
}
我也尝试过:
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"tsconfigRootDir": "__dirname",
"project": ["./tsconfig.json"]
},
没有效果。
我写了一篇关于此错误的潜在解决方案的博客文章,并在此处获取了相关部分。当规则来自插件时,您应该为规则名称添加前缀。
@typescript-eslint
在这种情况下。
{
"rules": {
- "no-floating-promises": "warn"
+ "@typescript-eslint/no-floating-promises": "warn"
}
}
您编写的配置还有两个问题。它们可能是写帖子时的拼写错误,因为您提到的错误与它们无关。
parserOptions.project
。 (您在第二个示例中修复了该问题)。如果您从配置扩展或使用检查类型的规则,则需要 parserOptions.project
,因为它们需要 TypeScript 进行类型检查。您使用plugin:@typescript-eslint/recommended-type-checked
。当 TypeScript 配置文件以默认的 tsconfig.json
命名时,您可以使用布尔值。no-floating-promises
添加到根目录。应该在rules
。另外,考虑使用“错误”而不是“警告”。
eslint
命令可能不会退出并显示错误代码“警告”(取决于命令行参数)。
以下是最终配置:
{
"env": {
"es2021": true,
"node": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-type-checked"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"project": true
},
"plugins": ["@typescript-eslint"],
"rules: {
"@typescript-eslint/no-floating-promises": "warn"
}
}