Conditional Type (์กฐ๊ฑด๋ถ€ ํƒ€์ž…)์ด๋ž€ ์ž…๋ ฅ๋œ ์ œ๋„ค๋ฆญ ํƒ€์ž…์— ๋”ฐ๋ผ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋งํ•œ๋‹ค

์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

// ์ œ๋„ค๋ฆญ์ด string์ด๋ฉด ๋ฌธ์ž์—ด๋ฐฐ์—ด, ์•„๋‹ˆ๋ฉด ๋„˜๋ฒ„๋ฐฐ์—ด
type IsStringType<T> = T extends string ? string[] : number[];
 
type T1 = IsStringType<string>; // type T1 = string[]
type T2 = IsStringType<number>; // type T2 = number[]
 
const a: T1 = ['ํ™๊ธธ๋™', '์ž„๊บพ์ •', '๋ฐ•ํ˜๊ฑฐ์„ธ'];
const b: T2 = [1000, 2000, 3000];
// ์ œ๋„ค๋ฆญ `T`๋Š” `boolean` ํƒ€์ž…์œผ๋กœ ์ œํ•œ.
// ์ œ๋„ค๋ฆญ T์— true๊ฐ€ ๋“ค์–ด์˜ค๋ฉด string ํƒ€์ž…์œผ๋กœ, false๊ฐ€ ๋“ค์–ด์˜ค๋ฉด number ํƒ€์ž…์œผ๋กœ data ์†์„ฑ์„ ํƒ€์ž… ์ง€์ •
interface isDataString<T extends boolean> {
   data: T extends true ? string : number;
   isString: T;
}
 
const str: isDataString<true> = {
   data: 'ํ™๊ธธ๋™', // String
   isString: true,
};
 
const num: isDataString<false> = {
   data: 9999, // Number
   isString: false,
};

๋ถ„์‚ฐ ์กฐ๊ฑด๋ถ€ํƒ€์ž…

๋‹ค์Œ๊ณผ ๊ฐ™์ด union ํƒ€์ž…์„ ๋„ฃ์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” ๊ฐ ํƒ€์ž…์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋‹ค๋ฃฌ๋‹ค

type IsStringType<T> = T extends string ? 'yes' : 'no';
 
type T1 = IsStringType<string | number>;   // 'yes' | 'no'

string | number๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐ๊ฐ (string extends string ? 'yes' : 'no') | (number extends string ? 'yes' : 'no') ๋กœ ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค

ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๋ถ„์‚ฐ์€ ์กฐ๊ฑด๋ถ€ ํƒ€์ž…์—์„œ naked type parameter๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋งŒ ๋™์ž‘ํ•œ๋‹ค

(naked) type parameter๋Š” ์ œ๋„ค๋ฆญ T ์™€ ๊ฐ™์ด ์˜๋ฏธ๊ฐ€ ์—†๋Š” ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋งํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ,
๋งŒ์ผ ์ง์ ‘ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ๋ช…์‹œํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ์ œ๋„ค๋ฆญ T[] ์™€ ๊ฐ™์ด ๋ณ€ํš๋œ ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋ฉด, naked ๊ฐ€ ์•„๋‹ˆ๊ฒŒ ๋œ๋‹ค.

type T1 = (1 | 3 | 5 | 7) extends number ? 'yes' : 'no'; // naked ํƒ€์ž…์ด ์•„๋‹ˆ๋ผ์„œ ๋ถ„์‚ฐ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
type T2<T> = T extends number ? T[] : 'no'; // ์ œ๋„ค๋ฆญ T๋Š” naked ํƒ€์ž…์ด๋ผ ๋ถ„์‚ฐ์ด ๋œ๋‹ค.
type T3<T> = T[] extends number ? 'yes' : T[]; // ์ œ๋„ค๋ฆญ์ด์ง€๋งŒ T[] ์™€ ๊ฐ™์ด ๋ณ€ํ˜•๋œ ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” naked ํƒ€์ž…์ด ์•„๋‹ˆ๋ผ์„œ ๋ถ„์‚ฐ์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.
 
type T4 = T1; // "yes"
type T5 = T2<(1 | 3 | 5 | 7)>; // 1[] | 3[] | 5[] | 7[]
type T6 = T2<(1 | 3 | 5 | 7)>; // (1 | 3 | 5 | 7)[]

never ์˜ ๊ฒฝ์šฐ

๋งŒ์•ฝ ์กฐ๊ฑด๋ถ€ ํƒ€์ž…์˜ ๊ฒฐ๊ณผ๋กœ never๊ฐ€ ๋‚˜์™”์„ ๊ฒฝ์šฐ์—๋Š” ์ด ํƒ€์ž…์„ ์ œ์™ธ์‹œํ‚จ๋‹ค

type Never<T> = T extends number ? T : never;
 
type Types = number | string | object;
type T2 = Never<Types>; // type T2 = number

์ด๋ฅผ ์ด์šฉํ•ด Exclude์™€ Extract๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค

// ์œ ๋‹ˆ์˜จ ํƒ€์ž…์„ ๋ฐ›์•„ T์™€ U๋ฅผ ๋น„๊ตํ•ด U์™€ ๊ฒน์น˜๋Š” ํƒ€์ž…๋“ค์€ ์ œ์™ธํ•œ T๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž…
type My_Exclude<T, U> = T extends U ? never : T;
 
type T2 = My_Exclude<(1 | 3 | 5 | 7), (1 | 5 | 9)>; // U ์ œ๋„ค๋ฆญ(1 | 5 | 9)์— ์†ํ•ด์žˆ์ง€ ์•Š์€ 3 | 7 ๋งŒ ๋ฐ˜ํ™˜๋จ 
type T3 = My_Exclude<string | number | (() => void), Function>; // U ์ œ๋„ค๋ฆญ(Function)์— ์†ํ•ด์žˆ์ง€ ์•Š์€ string | number ๋งŒ ๋ฐ˜ํ™˜ ๋จ
// ์œ ๋‹ˆ์˜จ ํƒ€์ž…์„ ๋ฐ›์•„ T์™€ U๋ฅผ ๋น„๊ตํ•ด U์™€ ๊ฒน์น˜๋Š” ํƒ€์ž…๋“ค๋งŒ ์žฌ๊ตฌ์„ฑํ•ด T๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž…
type My_Extract<T, U> = T extends U ? T : never;
 
type T4 = My_Extract<(1 | 3 | 5 | 7), (1 | 5 | 9)>; // U ์ œ๋„ค๋ฆญ์—(1 | 5 | 9) ์†ํ•ด์žˆ๋Š” 1 | 5 ๋งŒ ๋ฐ˜ํ™˜๋จ
 
export {};

infer ํ‚ค์›Œ๋“œ

infer ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ข€ ๋” ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ์— ๋Œ€์‘ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. T extends infer U ? X : Y๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด ์ถ”๋ก ๋œ ๊ฐ’์„ U์— ํ• ๋‹นํ•œ๋‹ค

type Foo<T> = T extends { a: infer U, b: infer U } ? U : never;
type T10 = Foo<{ a: string, b: string }>;  // string
type T11 = Foo<{ a: string, b: number }>;  // string | number
type Bar<T> = T extends { a: (x: infer U) => void, b: (x: infer U) => void } ? U : never;
type T20 = Bar<{ a: (x: string) => void, b: (x: string) => void }>;  // string
type T21 = Bar<{ a: (x: string) => void, b: (x: number) => void }>;  // string & number

์ด๋ฅผ ์‚ฌ์šฉํ•ด ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ, ๋ฆฌํ„ด ํƒ€์ž…์„ ์ถ”๋ก ํ•  ์ˆ˜๋„ ์žˆ๋‹ค

function fn(num: number, str: string, bool: boolean): string | void {
   return num.toString();
}
 
type My_ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
type My_Parameters<T extends (...args: any) => any> = T extends (...args: infer R) => any ? R : never;
 
type Return_Type = My_ReturnType<typeof fn> // ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด ํƒ€์ž…์„ ๋ฐ˜ํ™˜
// type Return_Type = string | voi
 
type Parameters_Type = My_Parameters<typeof fn> // ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์˜ ํƒ€์ž…์„ ๋ฐ˜ํ™˜
// type Parameters_Type = [num: number, ste: string, bool: boolean]
 
const a: My_ReturnType<typeof fn> = 'Hello';
const b: My_Parameters<typeof fn> = [123, 'Hello', true];

๋ฏธ๋ฆฌ ์ •์˜๋œ ์กฐ๊ฑด๋ถ€ ํƒ€์ž…

TypeScript 2.8 ๋ถ€ํ„ฐ ์ถ”๊ฐ€๋œ ๋ฏธ๋ฆฌ ์ •์˜๋œ ์กฐ๊ฑด๋ถ€ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

  • Exclude<T, U>
  • Extract<T, U>
  • NonNullable<T>
  • ReturnType<T>
  • InstanceType<T>

์ฐธ๊ณ