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

PrismaService类型错误:string类型无法赋值给never类型

解决PrismaService中$on("beforeExit")的类型错误问题

针对你遇到的Argument of type 'string' is not assignable to parameter of type 'never'错误,提供以下解决思路:

  • 确保Prisma CLI与客户端版本一致
    检查package.json中prisma和@prisma/client的版本是否完全相同,版本不匹配会导致类型定义异常。若不一致,执行以下命令同步版本:

    npm install prisma@latest @prisma/client@latest
    
  • 使用Prisma内置事件枚举代替字符串
    手动导入Prisma的事件枚举,调用$on时使用枚举值而非字符串,避免类型不匹配:

    import { PrismaClient, Prisma } from '@prisma/client';
    
    // ...
    
    async enableShutdownHooks(app: INestApplication) {
      this.$on(Prisma.Event.beforeExit, async () => {
        await app.close();
      });
    }
    
  • 清理依赖缓存并重新生成类型
    除了npx prisma generate,彻底清理缓存后重新安装依赖:

    rm -rf node_modules package-lock.json
    npm install
    npx prisma generate
    
  • 检查TypeScript配置
    确认tsconfig.json中strict模式相关设置,若开启严格类型检查导致类型问题,可尝试临时调整strictNullChecks等选项(更推荐优先解决类型根源),同时确保include字段包含Prisma生成的类型文件,比如:

    {
      "include": ["src/**/*", "prisma/**/*"]
    }
    
  • 类型断言兜底(不推荐但应急可用)
    若以上方法无效,可通过类型断言绕过类型检查:

    async enableShutdownHooks(app: INestApplication) {
      this.$on("beforeExit" as Prisma.Event.beforeExit, async () => {
        await app.close();
      });
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 11:32:37