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


当前回答

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

tsconfig.json:

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

然后重新加载IDE!

其他回答

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

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

将文件名添加到.eslintignore

or

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

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

您希望编译该文件

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

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

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

我只需要将路径添加到新的tsconfig。Json在项目数组和eslint脚本中,这个问题就消失了。

tsconfig.json:

project: ["./newfolder/tsconfig.json"]

package.json:

"eslint": "eslint -c ./.eslintrc.js './newfolder/**/*.ts'"

在我的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数组中包含该文件或文件的目录。

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

对我来说,这个问题发生在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": [""]
}