반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 프로그래머스파이썬연습문제
- 파이썬조합
- 파이썬문자열함수
- Python
- itertools
- aws
- Nest
- SAA
- 파이썬
- aws자격증가이드
- 프로그래머스 파이썬 문제풀이
- nest swagger
- 파이썬 sorted
- 파이썬 정렬
- examtopics
- 파이썬 math 라이브러리
- python sorted
- 파이썬 프로그래머스
- swagger 데코레이터
- nest swagger 데코레이터
- SAA자격증
- 프로그래머스 파이썬
- 프로그래머스파이썬
- EC2란
- 프로그래머스
- 파이썬 문자열 함수
- SAA-C03
- PHP8
- 파이썬 최소공배수
- Nest.js
Archives
- Today
- Total
사진찍는 개발자📸👩💻
[Nest.js] swagger 데코레이터 - 5 (타입 헬퍼) 본문
반응형
SMALL
타입 헬퍼란?
- 기존에 정의된 타입을 기반으로 새로운 타입을 쉽게 만들 수 있도록 도와주는 도구
- 코드 중복을 줄이고 타입간의 일관성을 유지하는 데에 유용
- 장점
- 코드 중복 감소
- 타입 안정성 보장
- API 문서 자동 생성
- 유지보수 용이성
- 일관된 DTO 구조 유지
1. PartialType()
- 용도: 모든 속성을 선택적(optional)으로 만듦
// 원본 DTO
class CreatePostDto {
@ApiProperty()
title: string;
@ApiProperty()
content: string;
@ApiProperty()
authorId: number;
}
// 모든 필드가 선택적으로 변환
@ApiTags('posts')
class UpdatePostDto extends PartialType(CreatePostDto) {}
// 결과:
// {
// title?: string;
// content?: string;
// authorId?: number;
// }
2. PickType()
- 용도: 특정 속성들만 선택해서 새로운 타입 생성
class CreateUserDto {
@ApiProperty()
email: string;
@ApiProperty()
password: string;
@ApiProperty()
name: string;
@ApiProperty()
age: number;
}
// email과 password만 선택
class LoginDto extends PickType(CreateUserDto, ['email', 'password']) {}
// 결과:
// {
// email: string;
// password: string;
// }
3. OmitType()
- 용도: 특정 속성들을 제외한 새로운 타입 생성
class UserDto {
@ApiProperty()
id: number;
@ApiProperty()
email: string;
@ApiProperty()
password: string;
}
// password 필드 제외
class UserResponseDto extends OmitType(UserDto, ['password']) {}
// 결과:
// {
// id: number;
// email: string;
// }
4. IntersectionType()
- 용도: 두 타입을 결합하여 새로운 타입 생성
class PostDto {
@ApiProperty()
title: string;
@ApiProperty()
content: string;
}
class AuditDto {
@ApiProperty()
createdAt: Date;
@ApiProperty()
updatedAt: Date;
}
// PostDto와 AuditDto 결합
class PostDetailDto extends IntersectionType(PostDto, AuditDto) {}
// 결과:
// {
// title: string;
// content: string;
// createdAt: Date;
// updatedAt: Date;
// }
반응형
LIST
'develop > Nest.js' 카테고리의 다른 글
[Nest.js] swagger 데코레이터 - 4 (DTO/모델) (0) | 2024.12.29 |
---|---|
[Nest.js] swagger 데코레이터 - 3 (보안) (0) | 2024.12.29 |
[Nest.js] swagger 데코레이터 - 2 (응답 데코레이터) (0) | 2024.12.29 |
[Nest.js] swagger 데코레이터 - 1 (0) | 2024.12.29 |