首页 > 解决方案 > 如何告诉打字稿给定的输入将具有预期类型的​​子集

问题描述

我正在使用带有 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])
}

从显示的错误中,可以肯定肯定会出现一个数字。但是有没有办法告诉打字稿只需要一个数字而不必编写额外的代码?或以任何方式更改自动生成的文件?

标签: typescriptnestjsprisma

解决方案


MainCategoryUncheckedUpdateInput您可以使用生成的id字段类型定义来创建新类型number

type customCategoryUpdateInput = Omit<Prisma.MainCategoryUncheckedUpdateInput, "id"> & {
    id?: number; // redifining id to be of type number only 
};

omit 实用程序用于在重新定义之前先擦除idin的原始类型定义MainCategoryUncheckedUpdateInput。现在您可以在服务文件中使用customCategoryUpdateInput代替。Prisma.MainCategoryUncheckedUpdateInput


推荐阅读