"parserOptions.project"已设置为@typescript-eslint/parser。
"parserOptions.project"已设置为@typescript-eslint/parser。
我用--template typescript
创建了一个新的React Native项目。
我删除了作为脚手架的template
目录。
然后我继续添加ESLint:
module.exports = { parser: "@typescript-eslint/parser", plugins: ["@typescript-eslint"], extends: ["airbnb-typescript-prettier"] };
但是,当我打开babel.config.js
时,我遇到了这个错误:
解析错误:\"parserOptions.project\" 已经被设置在@typescript-eslint/parser中。
该文件与你项目的配置不匹配:
/Users/Dan/site/babel.config.js
。该文件必须至少被包含在提供的项目之一。
您可以创建一个单独的TypeScript配置文件(tsconfig.eslint.json
)用于eslint
配置。该文件扩展了tsconfig
配置,并为需要逐行检查的文件设置了include
键。
.eslint.js
:
// ... parserOptions: { // ... project: "./tsconfig.eslint.json", // ... }, // ...
tsconfig.eslint.json
:
{ "extends": "./tsconfig.json", "include": [ // ... "babel.config.js" ] }
或者如果你想忽略它,你可以把它放到.eslintignore
中。
.eslintignore
:
// ... babel.config.js
JavaScript和TypeScript文件的不同的Lint规则
出现问题的原因可以是以下之一:
- 您正在使用一个需要类型信息的规则而没有指定
parserOptions.project
; - 您已经指定了
parserOptions.project
,但没有指定createDefaultProgram
(它将在未来的版本中被删除),并且您正在检测未包含在项目中的文件(例如babel.config.js
,metro.config.js
)
parserOptions.project
此选项允许您提供到您项目的
tsconfig.json
的路径。 如果您想使用需要类型信息的规则,则此设置是必需的。(...)
请注意,如果已指定此设置,而未指定
createDefaultProgram
,则您必须仅检查由提供的tsconfig.json
文件定义的项目中包括的文件。如果您现有的配置没有包含所有要检查的文件,则可以创建单独的tsconfig.eslint.json
。
为解决此问题,请将您的ESLint配置更新为仅在TypeScript文件上使用TypeScript规则:
{ // ... parser: '@typescript-eslint/parser', plugins: ["@typescript-eslint"], overrides: [ { files: ['*.ts', '*.tsx'], // Your TypeScript files extension // As mentioned in the comments, you should extend TypeScript plugins here, // instead of extending them outside the `overrides`. // If you don't want to extend any rules, you don't need an `extends` attribute. extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended-requiring-type-checking', ], parserOptions: { project: ['./tsconfig.json'], // Specify it only for TypeScript files }, }, ], // ... }
您可以在官方文档中阅读更多关于overrides
配置的内容:overrides如何工作?
不要检查特定的文件
如果您不想检查错误中提到的文件(例如babel.config.js
),您可以将其名称添加到.eslintignore
文件中进行忽略:
babel.config.js
在这种情况下,上面提到的步骤(关于为TypeScript文件覆盖配置)非常重要,因为您的项目可能包含想要检查的JavaScript和TypeScript文件。
您还可以为不同的情况创建其他overrides,例如测试文件的不同配置,因为可以使用开发者依赖项并在node
环境中运行,而不是在browser
中运行。