[Nest.js] PickType, OmitType

Woong·2026년 3월 24일

Nest.js, Node.js

목록 보기
32/32

PickType

  • 원하는 필드만 골라서 상속

  • ex) email, name만 골라서 상속

    • 원본 클래스에 정의된 Validation Decorator(예: @IsString)까지 그대로 상속받아 사용 가능
    • as const를 붙여 리터럴 타입임을 명시해야.
export class ReadonlyCatDto extends PickType(Cat, ['email', 'name'] as const) {
  ...
}

OmitType

  • 제외할 필드를 지정하여 상속

    • 베이스 클래스에서 특정 필드만 제외하고 나머지 모든 필드를 상속
    • Entity 기반으로 DTO를 만들 때, idcreatedAt처럼 클라이언트로부터 직접 입력받지 않는 필드를 제외하는 등
  • ex)

export class CreateCatDto extends OmitType(Cat, ['id'] as const) {
  ...
}

reference

0개의 댓글