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 | numbertype 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>