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
相关产品推荐
相关产品推荐

