我用——template typescript创建了一个新的React Native项目

我删除了作为样板的一部分的模板目录。

然后我继续添加ESLint:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["@typescript-eslint"],
  extends: ["airbnb-typescript-prettier"]
};

然而,当我打开babel.config.js时,我得到这个错误

解析错误:"parserOptions. "已经为@typescript-eslint/parser设置了Project "。 该文件与您的项目配置不匹配:/Users/Dan/site/babel.config.js。 该文件必须包含在至少一个提供的项目中。eslint


当前回答

在我的ESLint配置中,我有以下配置:

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  globals: {
    Promise: "readonly"
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"],
  },
  plugins: ["@typescript-eslint"],
  extends: [
    "eslint:recommended",
    "prettier/@typescript-eslint",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
  ],
}

更新 修复的关键部分是:

parser: "@typescript-eslint/parser"

这:

parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"], // could be tsconfig.json too
}

不要忘记在tsconfig.json的include数组中包含该文件或文件的目录。

其他回答

您需要将该文件添加到tsconfig include数组中。 更多细节请参见typescript-eslint/typescript-eslint#967。

我在VsCode中有这个问题,当我在文件夹结构中打开我的项目太高时。一个奇怪的解决方案,但它有效。

在我的例子中,Firestore函数项目的express In .ts,我必须从它创建的函数文件夹中打开它。

我应该注意到这是这类问题,因为'npm run-script lint'本身从未返回错误。

Linux -我的项目目录/home/user/projects是到/usr/share/dev/projects的符号链接我删除了符号链接和一切工作现在。

我得到的错误是告诉我无法找到任何。tsx文件在“../../../..”/{应用名称/路径}”

我希望我的故障排除时间能帮助到某人:)

这个错误来自VSCode扩展吗? 错误是否有一个相对路径,即使它在同一个文件夹(或子文件夹)中(如下面的错误消息所示)?

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: ../../../../../home/<ommitted>/projects/floor-planner/app/src/App.vue.
The file must be included in at least one of the projects provided.

如果是,使用CLI时检查是否仍然存在。

你可以从你的项目根目录运行eslint .. 如果没有发现这些错误,则很可能是通过符号链接打开了项目。

截至2021年8月,Eslint扩展不支持Symlinks。

在项目目录中,运行pwd -P来获取绝对路径。

在我的例子中,这导致

~/projects/floor-planner/app: pwd -P
/media/projects/2021-Q3/floor-planner/app

通过此路径打开项目。

Eslint应该不再显示错误。

对我来说,问题源于tsconfig中使用exclude属性忽略的一些文件,但eslint并没有忽略它们。

通常,如果有人想要TSC忽略文件,那么同样的方法也会应用到eslint中。所以只需复制粘贴.tsconfig配置中的exclude值到.eslintrc配置中的ignorePatterns属性。