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


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

你可以为eslint配置创建一个单独的TypeScript配置文件(tsconfig.eslint.json)。该文件扩展了tsconfig配置和设置,包括必须检测的文件的key。

.eslint.js:

// ...
parserOptions: {
  // ...
  project: "./tsconfig.eslint.json",
  // ...
},
// ...

tsconfig.eslint.json:

{
  "extends": "./tsconfig.json",
  "include": [
    // ...
    "babel.config.js"
  ]
}

或者如果你想忽略它,你可以把它放在。eslintignore。

eslintignore:

// ...
babel.config.js

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

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

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

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

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

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

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

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

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

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

tsconfig.json:

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

然后重新加载IDE!

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

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

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

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

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

在".eslintignore"中添加一行:

.eslintrc.js

对我来说,问题源于tsconfig中使用exclude属性忽略的一些文件,但eslint并没有忽略它们。

通常,如果有人想要TSC忽略文件,那么同样的方法也会应用到eslint中。所以只需复制粘贴.tsconfig配置中的exclude值到.eslintrc配置中的ignorePatterns属性。

我在添加'@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',
      },
    },
  ],
}

简单地通过在.eslintrc.js中添加ignorePatterns选项来指示eslint忽略它们:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.json',
  },
  ignorePatterns: ["babel.config.js"],
  ...

由于检测您的项目配置文件没有多大价值,您可以安全地忽略它们。

另外,我不会让它们成为你的tsconfig的一部分。Json或创建一个特殊的tsconfig.eslint.json文件,仅用于检测目的。

这个错误来自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中设置parserOptions如下所示

"parserOptions": {
  "ecmaFeatures": {
    "jsx": true
  },
  "ecmaVersion": 2019,
  "sourceType": "module"        
},

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

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

将文件名添加到.eslintignore

or

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

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

您希望编译该文件

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

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

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

我今天遇到了这个问题,上面的解决方案都没有帮助。我遇到的问题是,在同一个目录中有两个文件,它们共享相同的文件名(无扩展名)。例如,src / foo。ts和src/Foo.tsx。重命名其中一个文件修复了linter错误。看到@typescript-eslint /解析器# 955。

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

tsconfig.json:

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

package.json:

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

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

ignorePatterns: ['.eslintrc.js']

我的问题是在PHPStorm中,我设置了工作导览:

我清除了它,一切都工作了:\

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

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

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

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

Linux -我的项目目录/home/user/projects是到/usr/share/dev/projects的符号链接我删除了符号链接和一切工作现在。

我得到的错误是告诉我无法找到任何。tsx文件在“../../../..”/{应用名称/路径}”

我希望我的故障排除时间能帮助到某人:)

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.

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