Reputation: 330
I am using nestjs with prisma and after migrating the schema.prisma, many Prisma types got automatically generated. I am not sure about what exactly is the correct way to use the generated types but I am using the generated types on service file and my own Dto for controller for validations. However, this is giving problems like:
Type 'number | IntFieldUpdateOperationsInput' is not assignable to type 'number'. Type 'IntFieldUpdateOperationsInput' is not assignable to type 'number'.
this type IntFieldUpdateOperationsInput was autogenerated by prisma for the @default(autoincrement()). However, I don't think I even need this for my use case as only number type for id would suffice.
export type IntFieldUpdateOperationsInput = {
set?: number
increment?: number
decrement?: number
multiply?: number
divide?: number
}
category.service.ts
...
update({ id, name }: Prisma.MainCategoryUncheckedUpdateInput) {
return this.prismaService.mainCategory.update({
where: { id },
data: { name },
});
}
...
category.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])
}
From the error shown, it is certain that a number would is definitely expected. But is there a way of telling typescript that its okay to get just a number only without having to write extra code? or altering the auto generated file in any way?
Upvotes: 0
Views: 260
Reputation: 7268
You could create a new type using the generated definition of MainCategoryUncheckedUpdateInput
where the id
field is of type number
.
type customCategoryUpdateInput = Omit<Prisma.MainCategoryUncheckedUpdateInput, "id"> & {
id?: number; // redifining id to be of type number only
};
The omit utility is used to first erase the original type definition of id
in MainCategoryUncheckedUpdateInput
before redefining it. Now you can use customCategoryUpdateInput
in place of Prisma.MainCategoryUncheckedUpdateInput
in your service file.
Upvotes: 0