최대 1 분 소요

Mapped types

  • CRUD 같은 기능을 구축할 때 기본 엔티티 유형에 변형을 구성하는 것이 유용한 경우가 많다.
  • nest 에서는 여러 유틸리티 기능을 제공한다.

Partial

  • create 같은 경우는 다 필요하지만 update 같은 경우는 부분부분 필요한 경우가 있기 때문에, 사용하면 좋을 것 같다
import { ApiProperty } from '@nestjs/swagger';
export class CreateCatDto {
  @ApiProperty()
  name: string;

  @ApiProperty()
  age: number;

  @ApiProperty()
  breed: string;
}
export class UpdateCatDto extends PartialType(CreateCatDto) {}

Pick

  • 똑같은 속성에서 에서 가지고 오고 싶은 속성만 가져와서 사용 가능!
export class UpdateCatDto extends PickType(CreateCatDto, ['name', 'breed'] as const) {}

OmitType

  • 똑같은 속성에서 원하는 속성만 빼고 사용 할 수 있다.
export class UpdateCatDto extends OmitType(CreateCatDto, ['name'] as const) {}

Intersection Type

  • 내가 아는 방식대로 dto를 합쳐서 intersection 으로 만들어서 쓸 수 있다.
import { ApiProperty } from '@nestjs/swagger';

export class CreateCatDto {
  @ApiProperty()
  name: string;

  @ApiProperty()
  breed: string;
}

export class AdditionalCatInfo {
  @ApiProperty()
  color: string;
	
export class UpdateCatDto extends IntersectionType(
  CreateCatDto,
  AdditionalCatInfo,
) {}
}

회사 코드에 있는 DTO들로 구성을 해보았으나, nest-zod를 발견하게 되어 더욱 더 편해져서,,,, 결국 nest-zod를 사용하게 되었다…

nest docs