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

配置ESLint兼容JS/TS时遇parserOptions.project报错求助

解决JS文件中@typescript-eslint/naming-convention规则的parserServices错误

问题根源

你的报错核心原因:

  • @typescript-eslint/naming-convention 属于类型感知规则,必须通过parserOptions.project指定tsconfig生成parserServices才能正常运行。
  • 你的tsconfig配置中明确排除了所有JS文件(exclude字段里的**/*.js),导致ESLint处理JS文件时,TypeScript解析器无法从tsconfig中获取这些文件的类型信息,进而无法生成所需的parserServices。

解决方案

方案1:创建ESLint专用的tsconfig(推荐)

这种方式既能保留原tsconfig用于编译(排除JS文件),又能让ESLint正常处理所有JS/TS文件:

  1. 在项目根目录新建tsconfig.eslint.json文件:
{
  "extends": "./tsconfig.json",
  "include": [
    "./web/**/*.ts", "./web/**/*.tsx", "./web/**/*.js", "./web/**/*.jsx"
  ],
  "exclude": [
    "node_modules", "dist"
  ]
}

该配置继承原tsconfig的编译选项,但将JS/JSX文件加入include列表,并移除了对JS文件的排除规则。

  1. 更新ESLint配置(如.eslintrc.js),将parserOptions.project指向这个新配置:
module.exports = {
  // 其他基础配置
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.eslint.json', // 替换为ESLint专用的tsconfig
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: ['@typescript-eslint'],
  rules: {
    // 你的规则配置,包括@typescript-eslint/naming-convention
  }
};

方案2:调整原tsconfig并配置编译选项(不推荐,除非需要TS处理JS文件)

如果你愿意让TypeScript解析JS文件但不编译输出,可以修改原tsconfig:

  1. 在compilerOptions中添加以下配置:
"allowJs": true,
"noEmit": true

allowJs让TS识别JS文件,noEmit避免TS编译JS文件输出到dist目录。

  1. 修改include和exclude字段,移除对JS文件的排除:
"include": [
  "./web/**/*.ts", "./web/**/*.tsx", "./web/**/*.js", "./web/**/*.jsx"
],
"exclude": [
  "node_modules", "dist"
]
  1. 保持ESLint的parserOptions.project指向原tsconfig即可。

方案3:对JS文件禁用类型感知规则

如果你不需要对JS文件应用@typescript-eslint/naming-convention这类类型感知规则,可以在ESLint中通过overrides单独禁用:

module.exports = {
  // 其他基础配置
  overrides: [
    {
      files: ['*.js', '*.jsx'],
      rules: {
        '@typescript-eslint/naming-convention': 'off'
      }
    }
  ]
};

内容的提问来源于stack exchange,提问作者Antonio Gamiz Delgado

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:57:11