Programming/TypeScript

[TypeScript] ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž… Omit ์•Œ์•„๋ณด๊ธฐ

yuri lee 2024. 11. 28. 21:21
๋ฐ˜์‘ํ˜•

Intro

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์ธ Omit์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ ๋กœ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž… (Utility Types)์€ TypeScript์—์„œ ๊ธฐ์กด ํƒ€์ž…์„ ๋ณ€ํ˜•ํ•˜๊ฑฐ๋‚˜ ํ™•์žฅํ•˜์—ฌ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋‚ด์žฅ ํƒ€์ž…๋“ค์ž…๋‹ˆ๋‹ค. TypeScript๋Š” ๊ฐ•๋ ฅํ•œ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์ œ๊ณตํ•˜๋Š” ๋™์‹œ์—, ๋ณต์žกํ•œ ํƒ€์ž… ์ •์˜๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํƒ€์ž…๋“ค์€ ํŠน์ •ํ•œ ์ž‘์—…(์˜ˆ: ์„ ํƒ์  ํ•„๋“œ๋กœ ๋ณ€ํ™˜, ํŠน์ • ์†์„ฑ ์ œ๊ฑฐ ๋“ฑ)์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ฏธ๋ฆฌ ์ •์˜๋œ ํ…œํ”Œ๋ฆฟ์ฒ˜๋Ÿผ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

 

Omit ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

Omit์€ TypeScript์—์„œ ์ œ๊ณตํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜๋กœ, ํŠน์ • ๊ฐ์ฒด ํƒ€์ž…์—์„œ ์ง€์ •๋œ ํ‚ค๋ฅผ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์€ ๊ฐ์ฒด์˜ ์ผ๋ถ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋กœ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

Omit<Type, Keys>
  • Type: ๊ธฐ์กด ๊ฐ์ฒด ํƒ€์ž…
  • Keys: ์ œ์™ธํ•˜๊ณ ์ž ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ(ํ‚ค)๋“ค์˜ ์ง‘ํ•ฉ. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด๋‚˜ ์œ ๋‹ˆ์˜จ ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

 

Omit ๋™์ž‘ ๋ฐฉ์‹

Omit์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Omit์€ ๊ธฐ์กด ํƒ€์ž…์—์„œ ํŠน์ • ํ‚ค๋ฅผ ์ œ์™ธํ•œ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
  • keyof T: T ํƒ€์ž…์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 
  • Exclude<keyof T, K>: K์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 
  • Pick<T, ...>: ํŠน์ • ํ‚ค๋“ค๋งŒ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

 

Omit Example

1. ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ

interface User {
  id: number;
  name: string;
  email: string;
  age: number;
}

type UserWithoutEmail = Omit<User, "email">;

const user: UserWithoutEmail = {
  id: 1,
  name: "John",
  age: 30,
};

 

2. ์—ฌ๋Ÿฌ ํ‚ค ์ œ์™ธํ•˜๊ธฐ

type UserWithoutSensitiveInfo = Omit<User, "email" | "age">;

const user2: UserWithoutSensitiveInfo = {
  id: 2,
  name: "Alice",
};

 

์‹ค์šฉ์ ์ธ ํ™œ์šฉ ์‚ฌ๋ก€

1. API ์‘๋‹ต ํ•„ํ„ฐ๋ง: ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์™ธํ•œ ๊ฐ์ฒด ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ

type SafeUser = Omit<User, "email" | "password">;

 

2. ์ปดํฌ๋„ŒํŠธ Props ๊ด€๋ฆฌ: ํŠน์ • ์ปดํฌ๋„ŒํŠธ์—์„œ ์ผ๋ถ€ Props๋ฅผ ์ œ์™ธํ•œ ์ƒˆ๋กœ์šด Props ํƒ€์ž… ์ƒ์„ฑ

interface ButtonProps {
  onClick: () => void;
  color: string;
  disabled: boolean;
}

type ButtonWithoutOnClick = Omit<ButtonProps, "onClick">;

 

3. Redux ์•ก์…˜ ๋ฐ์ดํ„ฐ์—์„œ ๋ถˆํ•„์š”ํ•œ ํ•„๋“œ ์ œ๊ฑฐ

type ActionPayload = Omit<User, "id">;

 

 

Omit์€ ๊ธฐ์กด ํƒ€์ž…์—์„œ ํŠน์ • ํ‚ค๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์ตœ์ ํ™”๋œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง, ํƒ€์ž… ๊ด€๋ฆฌ, Props ์ •์˜ ๋“ฑ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ์–ด TypeScript ๊ฐœ๋ฐœ์—์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค :)

๋ฐ˜์‘ํ˜•