39-Mapped Types
1. Mapped Types 정리
Partial: 클래스의 프로퍼티 정의를 모두 optional로 만든다Pick: 특정 프로퍼티만 골라 사용 할 수 있다. (Omit의 반대)Omit: 특정 프로퍼티만 생략 할 수 있다. (Pick의 반대)Intersection: 두 타입의 프로퍼티를 모두 모아서 사용 할 수 있다Composition: Mapped Types를 다양하게 조합해서 중첩 적용 가능하다
2. Partial
typescript
1export class CreateUserDto {2@IsString()3readonly name: string45@IsEmail()6readonly email: string78@IsString()9readonly password: string10}
typescript
1export class UpdateUserDto extends PartialType(CreateUserDto) {}2/*3{4name?: string;5email?: string;6password?: string;7}8*/
3. Pick
typescript
1export class CreateUserDto {2@IsString()3readonly name: string45@IsEmail()6readonly email: string78@IsString()9readonly password: string10}
typescript
1export class LoginUserDto extends PickType(CreateUserDto, ['email', 'password'] as const) {}2/*3{4email: string;5password: string;6}7*/
4. Omit
typescript
1export class CreateUserDto {2@IsString()3readonly name: string45@IsEmail()6readonly email: string78@IsString()9readonly password: string10}
typescript
1export class PublicUserDto extends OmitType(CreateUserDto, ['password'] as const) {}2/*3{4name: string;5email: string;6}7*/
5. Intersection
typescript
1export class UserDetailDto {2@IsString()3readonly name: string45@IsEmail()6readonly email: string7}89export class AddressDto {10@IsString()11readonly street: string1213@IsString()14readonly city: string1516@IsString()17readonly country: string18}
typescript
1export class UserWithAddressDto extends IntersectionType(UserDetailDto, AddressDto) {}2/*3{4name: string;5email: string;6street: string;7city: string;8country: string;9}10*/
6. Composition
typescript
1export class UpdateCatDto extends PartialType(2OmitType(CreateCatDto, ['name'] as const), //3) {}