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

ESLint报Unexpected reserved word 'private'错误排查求助

排查ESLint报错:Unexpected reserved word 'private'

问题根源

你当前依赖的eslint-config-react-app默认配置没有正确启用TypeScript语法支持,导致ESLint把TS专属的**构造函数参数前加private**语法,误判成了JavaScript里的非法保留字。

解决步骤

  • 补全TS相关ESLint依赖
    项目需要安装处理TS语法的核心插件和解析器,执行:

    npm install --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
    
  • 修改package.json的ESLint配置
    更新eslintConfig节点,加入TypeScript支持的配置:

    "eslintConfig": {
      "extends": [
        "react-app",
        "react-app/jest",
        "plugin:@typescript-eslint/recommended"
      ],
      "parser": "@typescript-eslint/parser",
      "plugins": ["@typescript-eslint"]
    }
    
  • 确认IDE文件识别
    检查编辑器(比如VS Code)右下角是否显示.ts/.tsx,如果误识别为.js,手动切换文件类型。

  • 重启ESLint服务
    用编辑器的命令面板(VS Code按Ctrl+Shift+P/Cmd+Shift+P)输入ESLint: Restart ESLint Server,让新配置生效。

补充说明

构造函数参数前加private是TS的语法糖,作用是自动把参数声明为类的私有属性并完成赋值,纯JS的ESLint规则不认识这个语法,必须通过@typescript-eslint插件来解析。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:25:24