我用——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


当前回答

这个错误来自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应该不再显示错误。

其他回答

在本例中,我们有多个.eslintrc和多个tsconfig。Json在目录树中。(一个用于服务器,一个用于React客户端,一个用于。/client。)

这在CLI中工作得很好,但在VS Code的插件中就不行了。

修复方法是将./client/.eslintrc项目值设置为相对于根目录,而不是相对于.eslintrc文件。将“。/tsconfig. conf”改为“。/tsconfig. conf”。./client/tsconfig. Json“to”。json”,IDE检测工作如预期。

我有这个错误扔给我,即使项目设置包括在内。这是一个vuesfc,解决方案是缺少<script></script>模板。

JavaScript和TypeScript文件的lint规则不同

出现这个问题的原因如下:

您正在使用一个需要类型信息而没有指定的规则 parserOptions.project; 您指定了parserOptions。项目,没有指定createDefaultProgram(它将在未来的版本中删除),并且您正在检测项目中不包括的文件(例如babel.config.js, metro.config.js)

来自TypeScript ESLint解析器文档:

parserOptions.project This option allows you to provide a path to your project's tsconfig.json. This setting is required if you want to use rules which require type information. (...) Note that if this setting is specified and createDefaultProgram is not, you must only lint files that are included in the projects as defined by the provided tsconfig.json files. If your existing configuration does not include all of the files you would like to lint, you can create a separate 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
      },
    },
  ],
  // ...
}

你可以在官方文档上阅读关于覆盖配置的更多信息:如何覆盖工作?


不要检测特定的文件

如果你不想检测错误中提到的文件(例如babel.config.js),你可以忽略它,将它的名字添加到.eslintignore文件中:

babel.config.js

不管怎样,上面的步骤(关于覆盖TypeScript文件的配置)在你的项目包含你想要lint的JavaScript和TypeScript文件的情况下是重要的。

您还可以为不同的情况创建其他覆盖,例如为测试文件创建不同的配置,因为它可以使用开发人员依赖关系并在节点环境上运行,而不是在浏览器上运行。

Despite assurances that typescript definitely, absolutely, never caches anything, I suspected this was a caching issue after, when getting annoyed at incessant typescript errors while debugging and developing a feature, I modified my tsconfig.json to exclude my src directory temporarily. Upon reverting the change, and restarting my typescript server running through WebpackDevServer, I had this error, and got rid of it by doing yarn cache clean to clear my yarn cache, as well as rm -rf node_modules to delete my node modules folder. Then I re installed my packages with yarn and when I ran my devserver again, typescript/tslint no longer showed this error.

我碰巧有同样的问题一段时间,然后在添加.html到我的tsconfig的包含数组。Json文件,问题已被修复。

因为我在做一个Angular项目,所以在overrides中的files数组中,你可能会看到我的components扩展名,你可能需要根据项目的需要进行更改。

在eslintrc。Json文件,你需要有以下配置:

    "extends": [
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
        "plugin:@angular-eslint/recommended"
    ],
    "overrides": [
        {
            "files": ["*.ts", "*.component.html", "*.component.ts"],
            "parserOptions": {
                "project": ["./tsconfig.json"],
                "extraFileExtensions": [".html"]
            },
            "rules": { "@typescript-eslint/prefer-nullish-coalescing": "error" }
        }
    ],
    "parser": "@typescript-eslint/parser",

确保在tsconfig中有以下行。json文件:

"include": ["**/*.d.ts", "**/*.ts", "**/*.html", "**/*.scss", "**/*.css"],