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


当前回答

对我来说,这个问题发生在parserOptions中列出的项目时。项目扩展了基本的tsconfig。Json,它排除了这个文件。

删除extends: './tsconfig. txt。来自parserOptions中列出的项目的json。项目或从基本配置中删除exclude可以修复该问题。但是这种解决方法并不实用,因为我们需要扩展基本配置。

重写属性exclude将非常有帮助。

tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist",
    "types": ["jest"]
  },
  "include": ["**/*.spec.ts"],
  "exclude": [""]
}

其他回答

执行npm i -D @types/node 包括对*.config.js文件的typescript支持:

tsconfig.json:

{
  "include": [
    "**/*.config.js" // for *.config.js files
  ]
}

然后重新加载IDE!

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

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

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文件的情况下是重要的。

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

对我来说,这个问题发生在parserOptions中列出的项目时。项目扩展了基本的tsconfig。Json,它排除了这个文件。

删除extends: './tsconfig. txt。来自parserOptions中列出的项目的json。项目或从基本配置中删除exclude可以修复该问题。但是这种解决方法并不实用,因为我们需要扩展基本配置。

重写属性exclude将非常有帮助。

tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist",
    "types": ["jest"]
  },
  "include": ["**/*.spec.ts"],
  "exclude": [""]
}

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

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

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