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


当前回答

我在添加'@typescript-eslint/prefer- nulish -coalescing': 'error'到.eslintrc.js时也遇到了同样的问题。

经过大量的试验和错误,我想出了这个解决方案,在我的情况下很管用:

module.exports = {
  ...
  overrides: [
    {
      files: ['*.ts', '*.tsx'],
      parserOptions: {
        // this setting is required to use rules that require type information
        project: './tsconfig.json',
      },
      rules: {
        '@typescript-eslint/prefer-nullish-coalescing': 'error',
      },
    },
  ],
}

其他回答

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

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

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

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

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

将文件名添加到.eslintignore

or

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

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

您希望编译该文件

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

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

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

我使用一个符号链接从我的项目文件夹指向我的主文件夹:

/opt/project/workspace => ~/worspace .

使用符号链接路径~/workspace打开VSCode,错误始终存在。

使用原来的路径:/opt/project/workspace打开VSCode可以解决这个问题。

这应该不是问题,但现在确实是。

只需在.eslintrc.js文件中添加以下代码。

ignorePatterns: ['.eslintrc.js']

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

tsconfig.json:

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

package.json:

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