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

如何修复TypeScript枚举转字符串字面量联合类型时的ESLint报错:Parsing error: Type expected

解决ESLint报错:Parsing error: Type expected(枚举转模板字面量联合类型时)

这个问题我之前踩过一模一样的坑!你的代码本身是完全合规的TypeScript语法(从TS4.1开始支持通过${Kind}把枚举转为字符串字面量联合类型),报错的核心原因是你的ESLint TypeScript解析器版本太老,还不支持TS4.1引入的模板字面量类型特性。

下面是亲测有效的解决步骤:

  • 升级TypeScript ESLint相关依赖
    旧版本的@typescript-eslint/parser和@typescript-eslint/eslint-plugin无法识别模板字面量类型的语法,需要升级到支持TS4.1+的版本(推荐直接更到最新稳定版):

    # npm 升级
    npm update @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
    # yarn 升级
    yarn upgrade @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev
    
  • 检查ESLint配置文件
    确保你的.eslintrc.js(或其他格式的配置文件)里正确配置了TypeScript解析器,并且关联了正确的tsconfig.json:

    module.exports = {
      parser: '@typescript-eslint/parser',
      parserOptions: {
        // 指向你的tsconfig文件路径
        project: './tsconfig.json',
        ecmaVersion: 2020, // 至少设置到ES2020,避免语法兼容问题
      },
      plugins: ['@typescript-eslint'],
      extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended',
        'plugin:@typescript-eslint/recommended-requiring-type-checking',
      ],
    };
    
  • 清除ESLint缓存(可选)
    如果升级依赖后报错依然存在,可能是ESLint缓存了旧的解析规则,执行以下命令清除缓存:

    npx eslint --cache-clear
    

做完这些步骤,ESLint应该就能正确识别${Kind}这种模板字面量类型的语法了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 09:29:05