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


当前回答

经过大量的努力和尝试来纠正这个错误。我知道我的问题是我把忽略文件保存为eslintignore而不是。eslintignore。

因此,它不能忽略。js,。d。Ts文件dist被忽略。

其他回答

该错误意味着有一个文件可以编译,但不属于当前定义的项目结构。有几种解决方案:

如果你不关心文件是否编译(即。配置文件等)

将文件名添加到.eslintignore

or

将文件或文件模式添加到.eslintrc.js文件中

ignorePatterns: ["babel.config.js"]
// or 
ignorePatterns: ["**/*.config.js"]

您希望编译该文件

将文件、文件夹或模式添加到tsconfig中。json文件

include": [
    "src",
    "other_src",
]

说明部分修改需要重启IDE才能生效

我碰巧有同样的问题一段时间,然后在添加.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"],

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.

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