๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

 

• TypeScript๋Š” ๊ณตํ†ต ํƒ€์ž… ๋ณ€ํ™˜์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์„ ์ œ๊ณตํ•œ๋‹ค.

• ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์€ ์ „์—ญ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์ข…๋ฅ˜

  • Partial
  • Readonly
  • Record
  • Pick
  • Omit
  • Exclude
  • Extract
  • NonNullable
  • Parameter
  • ConstructorParameters
  • ReturnType
  • Required

 

1. Partial<T>

interface Todo {
  title: string;
  description: string;
}

function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
  return { ...todo, ...fieldsToUpdate };
}

const todo1 = {
  title: 'organize desk',
  description: 'clear clutter',
};

const todo2 = updateTodo(todo1, {
  description: 'throw out trash',
});

- ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ํƒ์ ์œผ๋กœ ๋งŒ๋“œ๋Š” ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

- ์ฃผ์–ด์ง„ ํƒ€์ž…์˜ ๋ชจ๋“  ํ•˜์œ„ ํƒ€์ž… ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

2. Readonly<T>

interface Todo {
  title: string;
}
const todo: Readonly<Todo> = {
  title: 'Delete inactive users',
};

// ์‚ฌ์šฉ ์˜ˆ: frozen ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ์žฌํ• ๋‹น ๋ฐฉ์ง€
todo.title = 'Hello';

// Error: Cannot as   sign to 'title' because it is a  read-only property

ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ๊ธฐ ์ „์šฉ(readonly)์œผ๋กœ ์„ค์ •ํ•œ ํƒ€์ž… ์„ ๊ตฌ์„ฑํ•œ๋‹ค.

๊ฐ์ฒด ์ƒ์„ฑ์‹œ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.

ํ”„๋กœํผํ‹ฐ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” const์ด๋‹ค. 

 

3. Record<T,K>

interface PageInfo {
  title: string;
}

type Page = 'home' | 'about' | 'contact';

const x: Record<Page, PageInfo> = {
  about: { title: 'about' },
  contact: { title: 'contact' },
  home: { subTitile: 'home' },
  // Error: '{ subTitile: string; }' is not assignable
  main: { title: 'home' },
  // Error: main is not assignable to type 'Page'.
};

ํ”„๋กœํผํ‹ฐ์˜ ์ง‘ํ•ฉ K๋กœ ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ๋“ค์„ ๋‹ค๋ฅธ ํƒ€์ž…์— ๋งคํ•‘์‹œํ‚ค๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

 

4. Pick<T,K>

interface Todo {
  title: string;
  description: string;
  completed: boolean;
  }

type TodoPreview = Pick<Todo, 'title' | 'completed'>;

const todo: TodoPreview = {
  title: 'Clean room',
  completed: false,
  description: 'description’
  // Error: 'description' is not assignable to type
};

ํ”„๋กœํผํ‹ฐ K์˜ ์ง‘ํ•ฉ์„ ์„ ํƒํ•ด ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค

 

5. Omit<T,K>

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Omit<Todo, 'description'>;

const todo: TodoPreview = {
  title: 'Clean room',
  completed: false,
  description: 'description', // Error: 'description' is not assignable to type
};

๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ํƒํ•œ ๋‹ค์Œ K๋ฅผ ์ œ๊ฑฐํ•œ ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

 

6. Exclude<T,U>

type T0 = Exclude<'a' | 'b' | 'c', 'a'>;
// "b" | "c"
type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>;
// "c"
type T2 = Exclude<string | number | (() => void), Function>;
// string | number

T์—์„œ U์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์†์„ฑ์„ ์ œ์™ธ ํ•œ ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

 

7. Extract<T,U>

type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>;
// "a"
type T1 = Extract<string | number | (() => void), Function>; 
// () => void

T์—์„œ U์— ํ• ๋‹น ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์†์„ฑ์„ ์ถ”์ถœ ํ•˜์—ฌ ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

 

8. NonNullable<T>

type T0 = NonNullable<string | number | undefined>;
// string | number
type T1 = NonNullable<string[] | null | undefined>;
// string[]

null ๊ณผ undefined๋ฅผ ์ œ์™ธํ•œ ํƒ€์ž…์ด๋‹ค.

 

9. Parameters<T>

declare function f1(arg: { a: number; b: string }): void;

type T0 = Parameters<() => string>; // []
type T1 = Parameters<(s: string) => void>; // [string]
type T2 = Parameters<<T>(arg: T) => T>; // [unknown]
type T4 = Parameters<typeof f1>; // [{ a: number, b: string }]
type T5 = Parameters<any>; // unknown[]
type T6 = Parameters<never>; // never
type T7 = Parameters<string>; // ์˜ค๋ฅ˜
type T8 = Parameters<Function>; // ์˜ค๋ฅ˜

ํ•จ์ˆ˜ ํƒ€์ž… T์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…๋“ค์˜ ํŠœํ”Œ ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

 

10. ConstructorParameters<T>

type T10 = ConstructorParameters<ErrorConstructor>; // [(string | undefined)?]
type T1 = ConstructorParameters<FunctionConstructor>; // string[]
type T2 = ConstructorParameters<RegExpConstructor>; // [string, (string | undefined)?]

interface I1 {
  new (args: string): Function;
}

type T12 = ConstructorParameters<I1>; // [string]

function f1(a: T12) {
  a[0];
  a[1]; // Error: Tuple type '[args: string]' of length '1' has no element at index '1'.
}

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํƒ€์ž…์˜ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์„ ์ถ”์ถœํ•œ๋‹ค.

๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” ํŠœํ”Œ ํƒ€์ž…(T๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ never)์„ ์ƒ์„ฑํ•œ๋‹ค.

 

11. ReturnType<T>

declare function f1(): { a: number; b: string };
type T0 = ReturnType<() => string>; // string
type T1 = ReturnType<(s: string) => void>; // void
type T2 = ReturnType<<T>() => T>; // {}
type T3 = ReturnType<<T extends U, U extends number[]>() => T>; // number[]
type T4 = ReturnType<typeof f1>; // { a: number,b: string }
type T5 = ReturnType<any>; // any
type T6 = ReturnType<never>; // any
type T7 = ReturnType<string>; // ์˜ค๋ฅ˜
type T8 = ReturnType<Function>; // ์˜ค๋ฅ˜

ํ•จ์ˆ˜ T์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ๊ตฌ์„ฑ๋œ ํƒ€์ž…์„ ์ƒ์„ฑํ•œ๋‹ค.

 

12. Required<T>

interface Props {
  a?: number;
  b?: string;
}

const obj: Props = { a: 5 };
const obj2: Required<Props> = { a: 5 };
// Error: Property 'b' is missing in type '{ a:number; }

T์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๊ฐ€ ํ•„์ˆ˜๋กœ ์„ค์ •๋œ ํƒ€์ž…์„ ๊ตฌ์„ฑํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•