typescript - 如何告诉打字稿给定的输入将具有预期类型的子集
问题描述
我正在使用带有 prisma 的 nestjs,并且在迁移 schema.prisma 后,许多 Prisma 类型会自动生成。我不确定使用生成类型的正确方法到底是什么,但我正在使用服务文件上的生成类型和我自己的 Dto 用于控制器进行验证。但是,这会带来以下问题:
输入'号码 | IntFieldUpdateOperationsInput' 不可分配给类型 'number'。类型“IntFieldUpdateOperationsInput”不可分配给类型“数字”。
这种类型的 IntFieldUpdateOperationsInput 是由 prisma 为 @default(autoincrement()) 自动生成的。但是,我认为我的用例甚至不需要这个,因为只有 id 的数字类型就足够了。
export type IntFieldUpdateOperationsInput = {
set?: number
increment?: number
decrement?: number
multiply?: number
divide?: number
}
类别.service.ts
...
update({ id, name }: Prisma.MainCategoryUncheckedUpdateInput) {
return this.prismaService.mainCategory.update({
where: { id },
data: { name },
});
}
...
类别.controller.ts
...
@Patch(':id')
update(
@Param('id') id: string,
@Body() updateCategoryDto: UpdateCategoryDto,
) {
return this.categoryService.update({ id: +id, ...updateCategoryDto });
}
...
Schema.prisma
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
username String @unique
password String
isAdmin Boolean
mainCategory MainCategory[]
projects Project[]
}
model TechStack {
id Int @id @default(autoincrement())
name String @unique
description String?
MainCategory MainCategory? @relation(fields: [mainCategoryId], references: [id])
mainCategoryId Int?
technologies Technology[]
}
model MainCategory {
id Int @id @default(autoincrement())
name String @unique
description String?
User User? @relation(fields: [userId], references: [id])
userId Int?
techStack TechStack[]
}
model Technology {
id Int @id @default(autoincrement())
name String @unique
description String?
status String
stars Int
TechStack TechStack? @relation(fields: [techStackId], references: [id])
techStackId Int?
projects ProjectTechnologies[]
}
model Project {
id Int @id @default(autoincrement())
name String @unique
description String?
link String
technologies ProjectTechnologies[]
User User? @relation(fields: [userId], references: [id])
userId Int?
}
model ProjectTechnologies {
project Project @relation(fields: [projectId], references: [id])
projectId Int // relation scalar field (used in the `@relation` attribute above)
technologies Technology @relation(fields: [technologyId], references: [id])
technologyId Int // relation scalar field (used in the `@relation` attribute above)
@@id([projectId, technologyId])
}
从显示的错误中,可以肯定肯定会出现一个数字。但是有没有办法告诉打字稿只需要一个数字而不必编写额外的代码?或以任何方式更改自动生成的文件?
解决方案
MainCategoryUncheckedUpdateInput
您可以使用生成的id
字段类型定义来创建新类型number
。
type customCategoryUpdateInput = Omit<Prisma.MainCategoryUncheckedUpdateInput, "id"> & {
id?: number; // redifining id to be of type number only
};
omit 实用程序用于在重新定义之前先擦除id
in的原始类型定义MainCategoryUncheckedUpdateInput
。现在您可以在服务文件中使用customCategoryUpdateInput
代替。Prisma.MainCategoryUncheckedUpdateInput
推荐阅读
- css - 使用 CSS 禁用输入字段 - 防止键盘操作
- python - 有没有办法获得 3d 矩阵的 2d 切片(嵌套列表)
- go - Go打包多个文件,如何构造
- build - 如何使用 CLion 将 C 代码闪存到 STM32 板上?
- jquery - 当它击中 WayPoint div 时如何定位动画项目
- reactjs - 如何将光标指针和其他css添加到材质ui datepicker
- node.js - 尝试通过nodejs启动browerstacklocal时出现本地错误
- python - 如何使用 python 获取 Excel 工作表的“总编辑时间”属性?
- sql - 如何退回每位作者出版的第一本书?
- javascript - 当您导航回它时更新已安装的屏幕