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

如何让VS Code内置ESLint识别tsconfig.json的compilerOptions.target配置

解决VS Code中ESLint报ts(2802)错误的方案

问题描述

ESLint报出如下错误:

Type 'this' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.

已做配置:

  • 在tsconfig.json中设置compilerOptions.target为es2015
  • 在package.json中添加ESLint配置:
"eslintConfig": {
  "env": {
    "node": true,
    "es2015": true
  }
}

但ESLint仍持续报ts(2802)错误,需让VS Code内置ESLint识别到target配置以停止报错。

解决方法

1. 让ESLint明确关联tsconfig.json

在ESLint配置文件(.eslintrc.js/.eslintrc.json)或package.json的eslintConfig字段中,添加parserOptions.project指向你的tsconfig.json,示例:

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": ["@typescript-eslint"]
}

此配置能让ESLint直接读取TypeScript的编译规则,而非仅依赖env设置。

2. 配置VS Code的ESLint插件

打开VS Code设置(快捷键Ctrl+,),搜索ESLint: Options,确认eslint.options包含parserOptions.project;或直接在.vscode/settings.json中添加:

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

3. 重启ESLint服务

打开VS Code命令面板(Ctrl+Shift+P),执行ESLint: Restart ESLint Server,让新配置生效。

4. 检查tsconfig.json有效性

确保tsconfig.json无语法错误,且compilerOptions.target确实设为es2015或更高版本,示例:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "strict": true
  }
}

5. 校验依赖版本兼容性

若使用@typescript-eslint相关包,确保@typescript-eslint/parser、@typescript-eslint/eslint-plugin的版本与TypeScript版本匹配,避免因版本不匹配导致配置读取异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 23:35:21