๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Back
NestJs
39-Mapped Types

1. Mapped Types ์ •๋ฆฌ

  • Partial : ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ ์ •์˜๋ฅผ ๋ชจ๋‘ optional๋กœ ๋งŒ๋“ ๋‹ค
  • Pick : ํŠน์ • ํ”„๋กœํผํ‹ฐ๋งŒ ๊ณจ๋ผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. (Omit์˜ ๋ฐ˜๋Œ€)
  • Omit : ํŠน์ • ํ”„๋กœํผํ‹ฐ๋งŒ ์ƒ๋žต ํ•  ์ˆ˜ ์žˆ๋‹ค. (Pick์˜ ๋ฐ˜๋Œ€)
  • Intersection : ๋‘ ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ชจ๋‘ ๋ชจ์•„์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค
  • Composition : Mapped Types๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ์กฐํ•ฉํ•ด์„œ ์ค‘์ฒฉ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค

2. Partial

1
export class CreateUserDto {
2
@IsString()
3
readonly name: string
4
5
@IsEmail()
6
readonly email: string
7
8
@IsString()
9
readonly password: string
10
}
1
export class UpdateUserDto extends PartialType(CreateUserDto) {}
2
/*
3
{
4
name?: string;
5
email?: string;
6
password?: string;
7
}
8
*/

3. Pick

1
export class CreateUserDto {
2
@IsString()
3
readonly name: string
4
5
@IsEmail()
6
readonly email: string
7
8
@IsString()
9
readonly password: string
10
}
1
export class LoginUserDto extends PickType(CreateUserDto, ['email', 'password'] as const) {}
2
/*
3
{
4
email: string;
5
password: string;
6
}
7
*/

4. Omit

1
export class CreateUserDto {
2
@IsString()
3
readonly name: string
4
5
@IsEmail()
6
readonly email: string
7
8
@IsString()
9
readonly password: string
10
}
1
export class PublicUserDto extends OmitType(CreateUserDto, ['password'] as const) {}
2
/*
3
{
4
name: string;
5
email: string;
6
}
7
*/

5. Intersection

1
export class UserDetailDto {
2
@IsString()
3
readonly name: string
4
5
@IsEmail()
6
readonly email: string
7
}
8
9
export class AddressDto {
10
@IsString()
11
readonly street: string
12
13
@IsString()
14
readonly city: string
15
16
@IsString()
17
readonly country: string
18
}
1
export class UserWithAddressDto extends IntersectionType(UserDetailDto, AddressDto) {}
2
/*
3
{
4
name: string;
5
email: string;
6
street: string;
7
city: string;
8
country: string;
9
}
10
*/

6. Composition

1
export class UpdateCatDto extends PartialType(
2
OmitType(CreateCatDto, ['name'] as const), //
3
) {}