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

使用TypeScript satisfies运算符时ESLint报‘Parsing error: ',' expected’如何解决?

问题描述

尝试使用TypeScript的satisfies运算符为变量保留最具体的类型,但在对象末尾添加satisfies Prisma.gameInclude;后,ESLint在对象的闭合大括号处抛出Parsing error: ',' expected.错误。

相关TypeScript代码:

const includeFields = {
  participants: true,
  queue: {
    include: {
      gamemode: {
        include: {
          character_pool: {
            include: {
              characters: true,
            },
          },
        },
      },
    },
  },
} satisfies Prisma.gameInclude;
// ^ Parsing error: ',' expected.

.eslintrc.js配置:

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
  overrides: [],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: "latest",
    sourceType: "module",
  },
  plugins: ["@typescript-eslint", "unused-imports"],
  rules: {
    "unused-imports/no-unused-imports": "error",
  },
};

已尝试在配置中添加parserOptions.project指向tsconfig但无效,当前使用的typescript-eslint版本为5.56.0(已知该版本支持satisfies运算符),需解决此解析错误。

解决方案
  • 检查TypeScript版本:satisfies是TypeScript 4.9.0及以上版本才支持的语法,确认项目中TypeScript版本≥4.9.0。typescript-eslint依赖TS编译器的语法支持,TS版本过低会导致解析失败。
  • 统一typescript-eslint包版本:确保项目中所有@typescript-eslint/parser、@typescript-eslint/eslint-plugin等相关包的版本完全一致(均为5.56.0),版本不匹配可能引发语法解析异常。
  • 清除ESLint缓存:运行eslint --cache-clear命令清除ESLint的缓存文件,避免旧的解析规则残留导致报错。
  • 更新IDE的ESLint插件:如果是在VS Code等IDE中出现错误,检查并更新IDE的ESLint插件至最新版本,确保插件能正确识别satisfies语法。
  • 验证tsconfig配置:在tsconfig.json中,确保compilerOptions.target设置为ES2021或更高版本,或者直接设置为ESNext,保证TypeScript编译器能正确处理该新语法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 12:22:50