You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启用tseslint类型检查并忽略eslint.config.js时出现ESLint错误

问题:TypeScript ESLint类型检查配置报错

尝试配置带类型检查的ESLint,添加...tseslint.configs.recommendedTypeChecked并忽略eslint.config.js后,出现以下错误:

Error: Error while loading rule '@typescript-eslint/await-thenable': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Parser: undefined
Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.
Occurred while linting /home/...
...

相关配置文件

eslint.config.js

// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier";
import globals from "globals";

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.recommendedTypeChecked,
  eslintConfigPrettier,
  {
    plugins: {
      "@typescript-eslint": tseslint.plugin,
    },
    languageOptions: {
      parser: tseslint.parser,
      parserOptions: {
        project: true,
        tsconfigRootDir: import.meta.dirname,
      },
      globals: {
        ...globals.node,
      },
    },
    ignores: ["**/dist/**", "**/node_modules/**", "**/eslint.config.js"],
    files: ["src/**/*.ts"],
  },
  {
    rules: {
      "@typescript-eslint/no-unused-vars": "warn",
    },
  },
);

tsconfig.json

{
  "extends": "@tsconfig/node20/tsconfig.json",
  "include": ["src"],
  "compilerOptions": {
    "outDir": "dist"
  }
}

prettier.config.js

/** @type {import("prettier").Config} */

const config = {
  experimentalTernaries: true,
  proseWrap: "always",
};

export default config;

注:忽略eslint.config.js是为了避免“ESLint was configured to run ... However, that TSConfig does not / none of those TSConfigs include this file”错误,参考官方FAQ及相关方案。


解决方案

错误核心是类型检查规则需要的parser配置未正确关联到目标文件,tseslint.configs.recommendedTypeChecked已内置@typescript-eslint/parser和插件,重复声明或配置拆分导致冲突。调整配置结构如下:

修改后的eslint.config.js

// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier";
import globals from "globals";

export default tseslint.config(
  // 基础JS推荐规则
  eslint.configs.recommended,
  // TypeScript类型检查推荐规则(已包含parser和plugin)
  ...tseslint.configs.recommendedTypeChecked,
  // 禁用与Prettier冲突的ESLint规则
  eslintConfigPrettier,
  // 全局配置:指定检查文件、全局变量、类型检查选项
  {
    files: ["src/**/*.ts"],
    languageOptions: {
      globals: {
        ...globals.node,
      },
      parserOptions: {
        project: true,
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
  // 自定义TS规则
  {
    files: ["src/**/*.ts"],
    rules: {
      "@typescript-eslint/no-unused-vars": "warn",
    },
  },
  // 全局忽略文件列表
  {
    ignores: ["**/dist/**", "**/node_modules/**", "**/eslint.config.js"],
  },
);

关键调整点

  • 移除重复的parser和plugin声明:tseslint.configs.recommendedTypeChecked已包含这些配置,重复声明会导致解析器冲突。
  • 拆分配置职责:将忽略文件列表单独放在一个配置对象中,确保全局生效;给自定义规则指定files范围,避免影响非TS文件。
  • 确保配置顺序合理:基础规则→扩展规则→自定义配置的顺序,保证规则正确继承。

验证

运行ESLint命令测试:

npx eslint src/

此时类型检查规则应正常工作,不再出现parser相关错误。


内容的提问来源于stack exchange,提问作者Vectoras

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 09:46:38