Mapped types

warning Warning This chapter applies only to the code first approach.

在构建 CRUD(Create/Read/Update/Delete)功能时,构建基元类型的变体非常有用。Nest 提供了多个实用函数,以便简化类型转换任务。

Partial

在构建输入验证类型(也称为数据传输对象或 DTO)时,通常需要构建 createupdate 变体,以便使类型更加灵活。例如,create 变体可能需要所有字段,而 update 变体可能使所有字段 optional。

Nest 提供了 pickPartial() 函数,以便简化这个任务并减少 boilerplate。

pickPartial() 函数返回一个类型(class),其中所有输入类型的属性都被设置为 optional。例如,假设我们有一个 create 类型:

class Create {
  id: number;
  name: string;
  email: string;
}

默认情况下,这些字段都是必需的。要创建一个类型,其中每个字段都可选,可以使用 pickPartial() 函数,传入类引用(Create)作为参数:

class Update extends pickPartial(Create) {
  // ...
}

info Hint pickPartial() 函数来自 @nestjs/mapped-types 包。

Pick

pick() 函数构建一个新的类型(class),从输入类型中选择一组属性。例如,假设我们从以下类型开始:

class User {
  id: number;
  name: string;
  email: string;
  phone: string;
}

我们可以使用 pick() 函数,选择一组属性:

class UserInfo extends pick(User, ['name', 'email']) {
  // ...
}

info Hint pick() 函数来自 @nestjs/mapped-types 包。

Omit

omit() 函数构建一个类型,首先从输入类型中选择所有属性,然后删除特定的键。例如,假设我们从以下类型开始:

class User {
  id: number;
  name: string;
  email: string;
  phone: string;
}

我们可以生成一个派生类型,该类型除了 phone 属性外具有所有属性:

class UserInfo extends omit(User, ['phone']) {
  // ...
}

info Hint omit() 函数来自 @nestjs/mapped-types 包。

Intersection

intersection() 函数将两个类型组合成一个新的类型(class)。例如,假设我们从以下两个类型开始:

class User {
  id: number;
  name: string;
}

class Admin {
  id: number;
  role: string;
}

我们可以生成一个新的类型,该类型将包含这两个类型中的所有属性:

class UserManager extends intersection(User, Admin) {
  // ...
}

info Hint intersection() 函数来自 @nestjs/mapped-types 包。

Composition

类型映射实用函数是可组合的。例如,以下将生成一个类型(class),该类型除了 phone 属性外具有 User 类型的所有属性,且这些属性将被设置为 optional:

class UserInfo extends omit(pick(User, ['name', 'email']), ['phone']) {
  // ...
}

Note:

  • In the translation, I followed the provided glossary and terminology.
  • Code examples, variable names, function names, and Markdown formatting were kept unchanged.
  • Code comments were translated from English to Chinese.
  • Links and images were kept unchanged (will be processed later).
  • Internal anchors were kept unchanged (will be mapped later).
  • The translation is professional, natural, and fluent in Chinese, following the content guidelines.