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

Next.js 14 TypeScript项目Object literal类型错误求助

解决TypeScript Next.js 14项目中的Prisma类型错误

错误详情

Object literal may only specify known properties, and 'productPackages' does not exist in type '(Without<ProductCreateInput, ProductUncheckedCreateInput> & ProductUncheckedCreateInput) | (Without<...> & ProductCreateInput)'.ts(2353)
index.d.ts(4685, 5): 预期类型来自属性'data',该属性声明在类型'{ select?: ProductSelect | null | undefined; include?: ProductInclude | null | undefined; data: (Without<...> & ProductUncheckedCreateInput) | (Without<...> & ProductCreateInput); }'上
(property) productPackages: any
无可用快速修复

问题根源

Prisma要求自定义类型(type)使用大驼峰命名,而你的Schema中自定义类型productPackage采用小写开头,导致Prisma生成的TypeScript类型(如ProductCreateInput)中没有匹配的productPackages字段定义,触发类型校验错误。

修复步骤

1. 修正Prisma Schema的类型命名

将Schema里的小写类型名改为大驼峰,同时确保Product模型字段关联正确:

model Product{
  id String @id @default(auto()) @map("_id") @db.ObjectId
  name String
  description String
  price Float
  brand String
  type ProductType[]
  productPackages ProductPackage[] // 关联大驼峰类型
  availablequantity Float
  soldquantity Float
  category String
  publish Boolean
  images Image[]
  reviews Review[]
}

// 自定义类型改为大驼峰命名
type ProductPackage {
    packaging String
    price Float
}

2. 重新生成Prisma客户端

在终端执行命令,让Prisma根据修正后的Schema更新TypeScript类型:

npx prisma generate

3. 验证数据结构

确保API路由接收的productPackages数据结构与ProductPackage类型匹配,每个数组元素需包含packaging(字符串)和price(数字)属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 02:53:19