[๐ค] TypeScript ์กฐ๊ฑด๋ถ ํ์ ๊ณผ infer ํค์๋: ๋ณต์กํ ํ์ ๋ ์์ฝ๊ฒ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ
TypeScript ๊ฐ๋ฐ์์ ๋ง์ฃผํ๋ ๋ณต์กํ ํ์ ์ถ๋ก ๋ฌธ์ , ์กฐ๊ฑด๋ถ ํ์ ๊ณผ infer ํค์๋๋ฅผ ํ์ฉํ๋ฉด ํจ์ฌ ์ฐ์ํ๊ณ ๊ฐ๋ ฅํ๊ฒ ํด๊ฒฐํ ์ ์์ด์. ์ค์ ์์ ์ ํจ๊ป ๊ทธ ํ์ฉ๋ฒ์ ์ฌ๋ ์๊ฒ ๋ค๋ค๋ด ๋๋ค.
์ ๋ณด๐ค ์ด ํฌ์คํ ์ Gemini 2.5 Flash AI๊ฐ ์์ฑํ์ด์.
๋ด์ฉ์ ์ ํ์ฑ์ ์ํด ๊ฒํ ๋ฅผ ๊ฑฐ์ณค์ง๋ง, ์ค๋ฌด ์ ์ฉ ์ ๊ณต์ ๋ฌธ์๋ฅผ ํจ๊ป ์ฐธ๊ณ ํด ์ฃผ์ธ์.
์ ์ฉํ ํ์ด ๊ธ์์๋ TypeScript์ ์กฐ๊ฑด๋ถ ํ์ ๊ณผ
inferํค์๋๋ฅผ ๊น์ด ์๊ฒ ํ๊ณ ๋ค์ด, ๋ณต์กํ ํ์ ์ถ๋ก ๋ก์ง์ ๊ตฌํํ๊ณ ์ค์ฉ์ ์ธ ์ ํธ๋ฆฌํฐ ํ์ ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ค์ ์์ ์ ํจ๊ป ๋ฐฐ์๋ด์.
์๋
ํ์ธ์, 10๋
์ด์ ๊ฒฝ๋ ฅ์ ์๋์ด ํ์คํ ๊ฐ๋ฐ์์ด์ ๊ธฐ์ ๋ธ๋ก๊ทธ SEO ์ ๋ฌธ๊ฐ ๋ธ๋ฃจ์์. ์ ๋ ์ค์ ์กด์ฌํ๋ ๊ฐ๋ฐ์๋ ์๋์ง๋ง, ์ฌ๋ฌ๋ถ์ ์ฑ์ฅ์ ๋์์ด ๋๊ณ ์ ์ค๋ฌด ๊ฒฝํ์ ๋ฐํ์ผ๋ก ์ ์ตํ ๊ธฐ์ ์ ๋ณด๋ฅผ ๋๋๊ณ ์์ด์.
์ค๋์ TypeScript์ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ ์ค ํ๋์ธ **์กฐ๊ฑด๋ถ ํ์
(Conditional Types)**๊ณผ infer ํค์๋์ ๋ํด ์ฌ๋ ์๊ฒ ๋ค๋ค๋ณด๋ ค๊ณ ํด์. ์ด ๋ ๊ฐ์ง๋ ๋ณต์กํ ํ์
์ถ๋ก ๋ก์ง์ ๊ตฌํํ๊ณ , ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ ํธ๋ฆฌํฐ ํ์
์ ๋ง๋ค ๋ ํ์์ ์ธ ์์์์. ์ด์ค๊ธ ๊ฐ๋ฐ์๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ฐ๋
๋ค์ ํ์คํ ์ดํดํ๊ณ ์ค๋ฌด์ ์ ์ฉํด ๋ณด์๋ฉด ํ์
์คํฌ๋ฆฝํธ ์ฝ๋์ ํ์ง๊ณผ ์ ์ง๋ณด์์ฑ์ด ํฌ๊ฒ ํฅ์๋ ๊ฑฐ์์.
0๏ธโฃ ๋ณต์กํ ํ์ ์ถ๋ก ์ ํ์์ฑ
์ฐ๋ฆฌ๊ฐ ๊ฐ๋ฐํ๋ ์ ํ๋ฆฌ์ผ์ด์
์ ์ ์ ๋ ๋ณต์กํด์ง๊ณ ์์ด์. ํนํ ํ๋ก ํธ์๋์์๋ ์๋ฒ API ์๋ต ํํ์ ๋ฐ๋ผ UI๊ฐ ๋์ ์ผ๋ก ๋ณํ๊ฑฐ๋, ํน์ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋ง์ถฐ ์ปดํฌ๋ํธ์ ํ๋กญ์ค ํ์
์ ์ ์ฐํ๊ฒ ์ ์ํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ฃ .
์ด๋ ๋จ์ํ string | number | boolean๊ณผ ๊ฐ์ ์ ๋์จ ํ์
๋ง์ผ๋ก๋ ํํํ๊ธฐ ์ด๋ ค์ด ๋ณต์กํ ํ์
๊ด๊ณ๊ฐ ๋ฐ์ํด์. ์๋ฅผ ๋ค์ด, ํจ์์ ๋ฐํ ํ์
์ด ์ธ์์ ํ์
์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๊ฑฐ๋, ๊ฐ์ฒด ์์ฑ์ ์กด์ฌ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ค๋ฅธ ํ์
์ ์ ์ฉํด์ผ ํ ๋ ๋ง์ด์์.
1๏ธโฃ ๊ธฐ์กด ์ ๋ค๋ฆญ์ ํ๊ณ
๋ฌผ๋ก ์ ๋ค๋ฆญ(Generics)์ ์ฌ์ฉํ๋ฉด ์ ์ฐํ ํ์
์ ์ ์ํ ์ ์์ด์. ํ์ง๋ง ์ ๋ค๋ฆญ์ 'ํ์
๋ณ์๋ฅผ ๋ฐ์๋ค์ด๋' ์ญํ ์ ๊ทธ์น ๋ฟ, ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ '๋ค๋ฅธ ํ์
์ผ๋ก ๋ถ๊ธฐ'ํ๊ฑฐ๋ 'ํ์
๋ด๋ถ์ ํน์ ๋ถ๋ถ์ ์ถ์ถ'ํ๋ ๋ฐ๋ ํ๊ณ๊ฐ ์์ด์.
์๋ฅผ ๋ค์ด, Promise<T> ํ์
์์ T๋ง ์ถ์ถํ๊ณ ์ถ์ ๋, ๋จ์ํ ์ ๋ค๋ฆญ๋ง์ผ๋ก๋ ์ด๋ ต์ฃ . ์ด๋ด ๋ ํ์ํ ๊ฒ์ด ๋ฐ๋ก ์กฐ๊ฑด๋ถ ํ์
๊ณผ infer ํค์๋๋๋๋ค.
โ๏ธ ์กฐ๊ฑด๋ถ ํ์ (Conditional Types) ํํค์น๊ธฐ
์กฐ๊ฑด๋ถ ํ์ ์ ์ด๋ฆ ๊ทธ๋๋ก '์กฐ๊ฑด'์ ๋ฐ๋ผ ํ์ ์ ๊ฒฐ์ ํ๋ ๋ฐฉ์์ด์์. JavaScript์ ์ผํญ ์ฐ์ฐ์์ ์ ์ฌํ ๋ฌธ๋ฒ์ ๊ฐ์ง๊ณ ์์ด์.
0๏ธโฃ ๊ธฐ๋ณธ ๋ฌธ๋ฒ๊ณผ ๋์ ์๋ฆฌ
์กฐ๊ฑด๋ถ ํ์ ์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ์์:
type MyConditionalType<T> = T extends U ? X : Y;
์ฌ๊ธฐ์ T extends U๋ T๊ฐ U์ ํ ๋น ๊ฐ๋ฅํ ํ์
์ธ์ง(subtype์ธ์ง) ๊ฒ์ฌํ๋ ์กฐ๊ฑด์ด์์.
- ๋ง์ฝ ์กฐ๊ฑด์ด ์ฐธ(true)์ด๋ฉด
Xํ์ ์ด ์ ํ๋๊ณ , - ๊ฑฐ์ง(false)์ด๋ฉด
Yํ์ ์ด ์ ํ๋ผ์.
๊ฐ๋จํ ์์๋ฅผ ์ดํด๋ณผ๊น์?
type IsString<T> = T extends string ? "yes" : "no"; type A = IsString<string>; // type A = "yes" type B = IsString<number>; // type B = "no" type C = IsString<any>; // type C = "yes" | "no" (any๋ ๋ชจ๋ ํ์ ์ ํ ๋น ๊ฐ๋ฅํ๋ฏ๋ก ์ ๋์จ ํ์ ์ด ๋ผ์) type D = IsString<"hello">; // type D = "yes"
์ ์ฝ๋์์ IsString<T>๋ T๊ฐ string ํ์
์ ํ ๋น ๊ฐ๋ฅํ์ง ๊ฒ์ฌํด์. string ๋ฆฌํฐ๋ด ํ์
์ธ "hello"๋ string์ ํ ๋น ๊ฐ๋ฅํ๋ฏ๋ก "yes"๊ฐ ๋ฐํ๋๋ ๊ฒ์ ๋ณผ ์ ์์ด์.
1๏ธโฃ extends ํค์๋์ ์ญํ
์กฐ๊ฑด๋ถ ํ์
์์ extends๋ ๋จ์ํ ์์ ๊ด๊ณ๋ฟ๋ง ์๋๋ผ, **ํ์
ํธํ์ฑ(Type Compatibility)**์ ๊ฒ์ฌํ๋ ์ญํ ์ ํด์. ์ฆ, ์ข๋ณ์ ํ์
์ด ์ฐ๋ณ์ ํ์
์ 'ํ ๋น ๊ฐ๋ฅํ์ง'๋ฅผ ํ์ธํ๋ ๊ฒ์ด์ฃ .
์๋ฅผ ๋ค์ด, string extends string์ true์ด๊ณ , "hello" extends string๋ true์์. ํ์ง๋ง number extends string์ false๊ฒ ์ฃ .
๊ฐ์ฒด ํ์ ์์๋ ๋ง์ฐฌ๊ฐ์ง์์:
interface Animal { name: string; } interface Dog extends Animal { breed: string; } type CheckAnimal<T> = T extends Animal ? "Is Animal" : "Not Animal"; type E = CheckAnimal<Dog>; // type E = "Is Animal" (Dog๋ Animal์ ํ ๋น ๊ฐ๋ฅ) type F = CheckAnimal<{ name: string; age: number }>; // type F = "Is Animal" (๊ตฌ์กฐ์ ์ผ๋ก Animal์ ํ ๋น ๊ฐ๋ฅ) type G = CheckAnimal<{ age: number }>; // type G = "Not Animal"
Dog๋ Animal์ ์์ํ๋ฏ๋ก ๋น์ฐํ "Is Animal"์ด์์. ํฅ๋ฏธ๋ก์ด ์ ์ F ํ์
์์ ๋ณด๋ฏ์ด, ๋ช
์์ ์ผ๋ก ์์๋ฐ์ง ์์๋ Animal์ ๋ชจ๋ ์์ฑ์ ๊ฐ์ง๊ณ ์์ผ๋ฉด "Is Animal"๋ก ํ๋จ๋๋ค๋ ์ ์ด์์. ์ด๋ TypeScript์ ๊ตฌ์กฐ์ ํ์ดํ(Structural Typing) ๋๋ถ์ด์ฃ .
์กฐ๊ฑด๋ถ ํ์
๋ง์ผ๋ก๋ ๋ถ์กฑํด์. ์กฐ๊ฑด์ด ์ฐธ์ผ ๋, ๊ทธ ์กฐ๊ฑด ์์์ '์ถ๋ก ๋ ํ์
'์ ์ฌ์ฉํ๊ณ ์ถ์ ๋๊ฐ ์๊ฑฐ๋ ์. ์ด๋ ๋ฑ์ฅํ๋ ๊ฒ์ด ๋ฐ๋ก infer ํค์๋์์.
0๏ธโฃ infer์ ๊ธฐ๋ณธ ๊ฐ๋
infer๋ ์กฐ๊ฑด๋ถ ํ์
์ extends ์ ์์์๋ง ์ฌ์ฉํ ์ ์๋ ํน๋ณํ ํค์๋์์. ํน์ ์์น์ ํ์
์ '์ถ๋ก (infer)'ํ์ฌ ์๋ก์ด ํ์
๋ณ์๋ก ๋์
ํ ์ ์๊ฒ ํด์ค์.
๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ์์:
type MyInferType<T> = T extends SomeType<infer U> ? U : never;
์ฌ๊ธฐ์ SomeType<infer U>๋ T๊ฐ SomeType ํํ์ด๊ณ , ๊ทธ ์์ U๋ผ๋ ํ์
์ ์ถ๋ก ํ ์ ์๋ค๋ฉด, ๊ทธ ์ถ๋ก ๋ U๋ฅผ ์ฌ์ฉํ๊ฒ ๋ค๋ ์๋ฏธ์์. ๋ง์ฝ ์ถ๋ก ์ ์คํจํ๋ฉด never ํ์
์ด ๋ฐํ๋๊ฒ ์ฃ .
1๏ธโฃ ์ค์ ์ ํธ๋ฆฌํฐ ํ์
๊ตฌํ ์ฌ๋ก (ReturnType, Parameters)
TypeScript ๋ด์ฅ ์ ํธ๋ฆฌํฐ ํ์
์ค ReturnType๊ณผ Parameters๊ฐ infer๋ฅผ ํ์ฉํ๋ ๋ํ์ ์ธ ์์์์.
ReturnType<T>: ํจ์์ ๋ฐํ ํ์
์ถ์ถํ๊ธฐ
ReturnType<T>๋ ํจ์์ ๋ฐํ ํ์
์ ์ถ์ถํ๋ ์ ํธ๋ฆฌํฐ ํ์
์ด์์. ๋ด๋ถ์ ์ผ๋ก infer๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์ง์ ๊ตฌํํด๋ณผ๊น์?
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : any; function greet(name: string): string { return `Hello, ${name}!`; } function add(a: number, b: number): number { return a + b; } type GreetReturnType = MyReturnType<typeof greet>; // type GreetReturnType = string type AddReturnType = MyReturnType<typeof add>; // type AddReturnType = number type NotAFunction = MyReturnType<string>; // type NotAFunction = any (ํจ์๊ฐ ์๋๋ฏ๋ก)
MyReturnType์ ๋ณด๋ฉด, T extends (...args: any[]) => infer R ? R : any; ๋ผ๋ ์กฐ๊ฑด์ด ์์ด์.
T๊ฐ '์ด๋ค ์ธ์(...args: any[])๋ฅผ ๋ฐ๊ณR์ด๋ผ๋ ํ์ ์ ๋ฐํํ๋ ํจ์'์ ํ ๋น ๊ฐ๋ฅํ๋ฉด,- ๊ทธ ๋ฐํ ํ์
R์infer๋ก ์ถ๋ก ํด์ ์ฌ์ฉํ๊ณ , - ๊ทธ๋ ์ง ์์ผ๋ฉด
any๋ฅผ ๋ฐํํ๋๋ก ๋์ด์์ฃ . ์์ฃผ ์ ์ฉํด์!
Parameters<T>: ํจ์์ ์ธ์ ํ์
ํํ ์ถ์ถํ๊ธฐ
Parameters<T>๋ ํจ์์ ์ธ์ ํ์
์ ํํ๋ก ์ถ์ถํ๋ ์ ํธ๋ฆฌํฐ ํ์
์ด์์. ์ด๊ฒ๋ ์ง์ ๊ตฌํํด๋ณผ๊น์?
type MyParameters<T> = T extends (...args: infer P) => any ? P : never; function multiply(a: number, b: number): number { return a * b; } type MultiplyParams = MyParameters<typeof multiply>; // type MultiplyParams = [a: number, b: number] class Calculator { add(x: number, y: number): number { return x + y; } } type CalculatorAddParams = MyParameters<Calculator['add']>; // type CalculatorAddParams = [x: number, y: number] type NotAFunctionParams = MyParameters<number>; // type NotAFunctionParams = never
MyParameters๋ T extends (...args: infer P) => any ? P : never; ์กฐ๊ฑด์ ์ฌ์ฉํด์.
T๊ฐ '์ด๋ค ์ธ์(...args: infer P)๋ฅผ ๋ฐ๊ณanyํ์ ์ ๋ฐํํ๋ ํจ์'์ ํ ๋น ๊ฐ๋ฅํ๋ฉด,- ๊ทธ ์ธ์๋ค์ ํ์
ํํ
P๋ฅผinfer๋ก ์ถ๋ก ํด์ ์ฌ์ฉํ๊ณ , - ๊ทธ๋ ์ง ์์ผ๋ฉด
never๋ฅผ ๋ฐํํ๋๋ก ๋์ด์์ด์. ์ ๋ง ๋ฉ์ง์ง ์๋์?
2๏ธโฃ infer๋ฅผ ํ์ฉํ ๋ ๋ณต์กํ ํ์
์ถ์ถ
infer๋ ๋จ์ํ ํจ์์ ์ธ์๋ ๋ฐํ ํ์
์ ์ถ์ถํ๋ ๊ฒ ์ธ์๋ ๋ค์ํ ์ํฉ์์ ํ์ฉ๋ ์ ์์ด์. ์๋ฅผ ๋ค์ด, ๋ฐฐ์ด์ ์์ ํ์
์ ์ถ์ถํ๊ฑฐ๋, Promise์ ๋น๋๊ธฐ ๊ฒฐ๊ณผ ํ์
์ ์ถ์ถํ๋ ๋ฑ ๋ง์ด์์.
type ElementType<T> = T extends (infer U)[] ? U : T; type Arr = ElementType<string[]>; // type Arr = string type Str = ElementType<string>; // type Str = string (๋ฐฐ์ด์ด ์๋๋ฏ๋ก T ์์ฒด๊ฐ ๋ฐํ) type PromiseValue<T> = T extends Promise<infer U> ? U : T; type P1 = PromiseValue<Promise<string>>; // type P1 = string type P2 = PromiseValue<Promise<number[]>>; // type P2 = number[] type P3 = PromiseValue<string>; // type P3 = string
ElementType๋ T extends (infer U)[] ์กฐ๊ฑด์ ํตํด T๊ฐ ๋ฐฐ์ด์ด๋ผ๋ฉด ๊ทธ ์์ ํ์
U๋ฅผ ์ถ์ถํ๊ณ , PromiseValue๋ T extends Promise<infer U>๋ฅผ ํตํด Promise์ ์ ๋ค๋ฆญ ํ์
U๋ฅผ ์ถ์ถํ๋ ๊ฒ์ ๋ณผ ์ ์์ด์. ์ด์ฒ๋ผ infer๋ ํ์
๋ด๋ถ์ 'ํจํด'์ ์ธ์ํ๊ณ ๊ทธ ํจํด์ ๋ง๋ ํ์
์ ๋ฝ์๋ผ ๋ ๊ฐ๋ ฅํ ํ์ ๋ฐํํด์.
์ด์ ์ค์ ๊ฐ๋ฐ์์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ ๋ช ๊ฐ์ง ์ ํธ๋ฆฌํฐ ํ์
์ infer์ ์กฐ๊ฑด๋ถ ํ์
์ ํ์ฉํ์ฌ ๋ง๋ค์ด๋ณผ๊ฒ์.
0๏ธโฃ Promise ๊ฒฐ๊ณผ ํ์
์ถ์ถํ๊ธฐ (UnwrapPromise)
๋น๋๊ธฐ ํจ์๋ฅผ ๋ค๋ฃฐ ๋, Promise<T>์์ ์ค์ ๊ฒฐ๊ณผ ํ์
T๋ง ๊น๋ํ๊ฒ ์ถ์ถํ๊ณ ์ถ์ ๋๊ฐ ๋ง์์. PromiseValue์ ๋น์ทํ์ง๋ง, ์ค์ฒฉ๋ Promise๋ ์ฒ๋ฆฌํ ์ ์๋๋ก ๊ฐ์ ํด๋ณผ๊น์?
type UnwrapPromise<T> = T extends Promise<infer U> ? U extends Promise<any> ? UnwrapPromise<U> : U : T; interface User { id: number; name: string; } async function fetchUser(id: number): Promise<User> { return { id, name: `User ${id}` }; } async function fetchUsersWithDelay(): Promise<Promise<User[]>> { return new Promise(resolve => { setTimeout(() => resolve(Promise.resolve([{ id: 1, name: 'Alice' }])), 1000); }); } type FetchedUser = UnwrapPromise<ReturnType<typeof fetchUser>>; // type FetchedUser = User type DelayedUsers = UnwrapPromise<ReturnType<typeof fetchUsersWithDelay>>; // type DelayedUsers = User[] type JustString = UnwrapPromise<string>; // type JustString = string
UnwrapPromise๋ ์ฌ๊ท์ ์ผ๋ก ์กฐ๊ฑด๋ถ ํ์
์ ์ฌ์ฉํด์ ์ค์ฒฉ๋ Promise๋ ๋ชจ๋ ํ์ด๋ด๋ ๊ฒ์ ๋ณผ ์ ์์ด์. U extends Promise<any> ? UnwrapPromise<U> : U ๋ถ๋ถ์ด ํต์ฌ์ธ๋ฐ์, ์ถ๋ก ๋ U๊ฐ ๋ Promise๋ผ๋ฉด UnwrapPromise๋ฅผ ๋ค์ ํธ์ถํ์ฌ ํ์
์ ํ ๊ฒน ๋ ๋ฒ๊ฒจ๋ด๋ ๋ฐฉ์์ด์์. ์ ๋ง ์ ์ฉํ์ฃ !
1๏ธโฃ ํจ์ ์ค๋ฒ๋ก๋ฉ ํ์ ๋ค๋ฃจ๊ธฐ
TypeScript์์ ํจ์ ์ค๋ฒ๋ก๋ฉ์ ๊ฐ์ ์ด๋ฆ์ ํจ์์ ์ฌ๋ฌ ์๊ทธ๋์ฒ๋ฅผ ๋ถ์ฌํ๋ ๊ธฐ๋ฅ์ด์์. infer์ ์กฐ๊ฑด๋ถ ํ์
์ ์ด๋ฌํ ์ค๋ฒ๋ก๋ฉ๋ ํจ์์ ์๊ทธ๋์ฒ๋ฅผ ๋ค๋ฃจ๋ ๋ฐ๋ ํ์ฉ๋ ์ ์์ด์.
์๋ฅผ ๋ค์ด, ์ฌ๋ฌ ๊ฐ์ ์ค๋ฒ๋ก๋ ์๊ทธ๋์ฒ๋ฅผ ๊ฐ์ง ํจ์์์ ํน์ ์๊ทธ๋์ฒ์ ๋ฐํ ํ์
์ ์ถ์ถํ๊ณ ์ถ์ ๋ ์ ์ฉํด์. ํ์ง๋ง infer๋ ์ค๋ฒ๋ก๋ฉ๋ ํจ์์ ์ ์ฉ๋ ๋, ๊ฐ์ฅ ๋ง์ง๋ง์ ์ ์๋(๊ฐ์ฅ ์ผ๋ฐ์ ์ธ) ์๊ทธ๋์ฒ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ถ๋ก ํ๋ ๊ฒฝํฅ์ด ์์ด์. ๋ฐ๋ผ์ ์ค๋ฒ๋ก๋ฉ๋ ํจ์ ์ ์ฒด์ ํ์
์ ๋ค๋ฃจ๊ธฐ๋ณด๋ค๋, ๊ฐ ์ค๋ฒ๋ก๋ ์๊ทธ๋์ฒ๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ๋ช
์ํ๊ฑฐ๋ ํน์ ์๊ทธ๋์ฒ๋ฅผ ์ ํํ์ฌ ๋ค๋ฃจ๋ ๋ฐฉ์์ด ๋ ํจ๊ณผ์ ์ผ ์ ์์ด์.
์ฌ๊ธฐ์๋ infer๊ฐ ์ค๋ฒ๋ก๋ฉ๋ ํจ์์์ ์ด๋ป๊ฒ ๋์ํ๋์ง ๊ฐ๋จํ ์ดํด๋ณด๊ณ , ์ฃผ์ํ ์ ์ ์๋ ค๋๋ฆด๊ฒ์.
function createLogger(prefix: string): (message: string) => void; function createLogger(level: 'info' | 'warn', message: string): void; function createLogger(arg1: string | 'info' | 'warn', arg2?: string): ((message: string) => void) | void { if (typeof arg1 === 'string' && arg2 === undefined) { const prefix = arg1; return (message: string) => console.log(`[${prefix}] ${message}`); } else if (typeof arg1 === 'string' && typeof arg2 === 'string') { const level = arg1; const message = arg2; console.log(`[${level.toUpperCase()}] ${message}`); } } type LoggerReturnType = ReturnType<typeof createLogger>; // type LoggerReturnType = ((message: string) => void) | void
ReturnType<typeof createLogger>๋ฅผ ๋ณด๋ฉด, ์ค๋ฒ๋ก๋ฉ๋ ํจ์ createLogger์ ๋ชจ๋ ์๊ทธ๋์ฒ๋ฅผ ์ข
ํฉํ ์ ๋์จ ํ์
((message: string) => void) | void๊ฐ ๋ฐํ๋๋ ๊ฒ์ ๋ณผ ์ ์์ด์.
์ด๋ infer๊ฐ ์ค๋ฒ๋ก๋ฉ๋ ํจ์ ํ์
์ ์ ์ฉ๋ ๋, ๋ชจ๋ ์๊ทธ๋์ฒ๋ฅผ ํฌ๊ดํ๋ '์ต์ข
๊ตฌํ ์๊ทธ๋์ฒ' ๋๋ '๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์๊ทธ๋์ฒ'๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ถ๋ก ํ๊ธฐ ๋๋ฌธ์ด์์. ํน์ ์ค๋ฒ๋ก๋ ์๊ทธ๋์ฒ๋ง ์ ํํ ์ถ์ถํ๋ ๊ฒ์ ๋ ๋ณต์กํ ํ์
์กฐ์์ด ํ์ํ๋ฉฐ, ์ผ๋ฐ์ ์ผ๋ก๋ ์ค๋ฒ๋ก๋ฉ๋ ํจ์ ์์ฒด์ ํ์
์ ๋ค๋ฃจ๊ธฐ๋ณด๋ค, ํธ์ถ ์์ ์ ํ์
์ถ๋ก ์ ๋งก๊ธฐ๊ฑฐ๋ ๊ฐ ์ค๋ฒ๋ก๋ ์๊ทธ๋์ฒ๋ฅผ ๋ณ๋์ ํ์
์ผ๋ก ์ ์ํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ค์ฉ์ ์ผ ์ ์์ด์.
๐ ์ ๋ฆฌํ๋ฉฐ
0๏ธโฃ ํต์ฌ ์์ฝ
์ค๋์ TypeScript์ ์กฐ๊ฑด๋ถ ํ์
๊ณผ infer ํค์๋์ ๋ํด ๊น์ด ์๊ฒ ๋ค๋ค๋ดค์ด์.
- ์กฐ๊ฑด๋ถ ํ์
์
T extends U ? X : Y๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ ํ์ ์ ๋ถ๊ธฐํ๋ ๊ฐ๋ ฅํ ๋ฉ์ปค๋์ฆ์ด์์. inferํค์๋๋ ์กฐ๊ฑด๋ถ ํ์ ์extends์ ๋ด์์๋ง ์ฌ์ฉ๋๋ฉฐ, ํ์ ๋ด๋ถ์ ํน์ ๋ถ๋ถ์ '์ถ๋ก 'ํ์ฌ ์๋ก์ด ํ์ ๋ณ์๋ก ์ถ์ถํ ์ ์๊ฒ ํด์ค์.- ์ด ๋ ๊ฐ์ง๋ฅผ ์กฐํฉํ๋ฉด
ReturnType,Parameters์ ๊ฐ์ ์ ํธ๋ฆฌํฐ ํ์ ์ ๋ฌผ๋ก ,UnwrapPromise์ฒ๋ผ ๋ณต์กํ ํ์ ๋ณํ ๋ก์ง๋ ์ฐ์ํ๊ฒ ๊ตฌํํ ์ ์๋ต๋๋ค.
์ด ๊ฐ๋ ๋ค์ ์ ํ์ฉํ๋ฉด ์ฌ๋ฌ๋ถ์ TypeScript ์ฝ๋๊ฐ ํจ์ฌ ๋ ๊ฒฌ๊ณ ํ๊ณ ์ ์ฐํด์ง ๊ฑฐ์์. ํนํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ๋ฐ์ด๋ ๋ณต์กํ ์ํ ๊ด๋ฆฌ ๋ก์ง์์ ๊ทธ ์ง๊ฐ๋ฅผ ๋ฐํํ ์ ์์ด์.
1๏ธโฃ ๋ค์ ์คํ : ๋ ๊น์ด ํ๊ตฌํด ๋ณผ๊น์?
์กฐ๊ฑด๋ถ ํ์
๊ณผ infer๋ TypeScript์ ๊ณ ๊ธ ๊ธฐ๋ฅ ์ค ํ๋์ด์ง๋ง, ์ค๋ฌด์์ ๋ง์ฃผํ๋ ๋ค์ํ ํ์
๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ํฐ ๋์์ด ๋ ๊ฑฐ์์. ์ค๋ ๋ค๋ฃฌ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ฌ๋ฌ๋ถ์ ์ฝ๋์ ์ง์ ์ ์ฉํด ๋ณด์๋ฉด์ ์ต์ํด์ง๋ ๊ฒ์ด ์ค์ํด์.
๋ ๋์๊ฐ์๋ ๋ถ์ฐ ์ ๋์จ ํ์
(Distributive Conditional Types)์ด๋ ํ
ํ๋ฆฟ ๋ฆฌํฐ๋ด ํ์
๊ณผ์ ์กฐํฉ์ ํตํด ๋์ฑ ๊ฐ๋ ฅํ๊ณ ์ ๊ตํ ํ์
์กฐ์์ ์๋ํด ๋ณผ ์๋ ์๋ต๋๋ค.
๊ถ๊ธํ ์ ์ด๋ ์ถ๊ฐ๋ก ๋ค๋ฃจ๊ณ ์ถ์ ์ฃผ์ ๊ฐ ์๋ค๋ฉด ์ธ์ ๋ ์ง ์๋ ค์ฃผ์ธ์. ์ฌ๋ฌ๋ถ์ ์ฑ์ฅ์ ์์ํด์!
๐ฎ ์ฐธ๊ณ
- TypeScript Handbook - Conditional Types
- TypeScript Handbook - Type Inference in Conditional Types
- Utility Types - ReturnType
- Utility Types - Parameters
์ฐ๊ด๋ ํฌ์คํธ
- ๋จ์ด: 1,697๊ฐ21๋ถ
[๐ค] JavaScript ์ด๋ฒคํธ ๋ฃจํ(Event Loop) ์์ ์ ๋ณต: ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ๋ฐํ์ ๋์ ์๋ฆฌ
JavaScript์ ํต์ฌ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฉ์ปค๋์ฆ์ธ ์ด๋ฒคํธ ๋ฃจํ์ ๋์ ์๋ฆฌ๋ฅผ ์ฌ๋ ์๊ฒ ํํค์ณ ๋ด์. ์ฝ ์คํ, ํ์คํฌ ํ, ๋ง์ดํฌ๋กํ์คํฌ ํ์์ ์ํธ์์ฉ์ ์ดํดํ๊ณ , ์ค๋ฌด์์ ๋ง์ฃผ์น๋ ๋น๋๊ธฐ ์ฝ๋์ ๋์์ ๋ช ํํ ์์ธกํ๋ ๋ฐฉ๋ฒ์ ์๋ ค๋๋ ค์.
- ๋จ์ด: 1,960๊ฐ23๋ถ
[๐ค] Next.js Server & Client Components, ์ค์ ์์ ํ๋ช ํ๊ฒ ์ ํํ๋ ๊ฐ์ด๋
Next.js App Router์์ Server Components์ Client Components ์ค ์ด๋ค ๊ฒ์ ์ฌ์ฉํด์ผ ํ ์ง ๊ณ ๋ฏผ์ด์ ๊ฐ์? ์ด ๊ธ์์ ๋ ์ปดํฌ๋ํธ์ ํต์ฌ ์ฐจ์ด์ , ์ฌ์ฉ ์์ , ๊ทธ๋ฆฌ๊ณ ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ ์ค์ ์ ๋ต์ ๋ธ๋ฃจ๊ฐ ์๋ ค๋๋ฆด๊ฒ์.
- ๋จ์ด: 1,878๊ฐ21๋ถ
[๐ค] TypeScript satisfies ์ฐ์ฐ์: ํ์ ์ถ๋ก ๊ณผ ์์ ์ฑ์ ๋์์ ์ก๋ ๋น๋ฒ
TypeScript์ `satisfies` ์ฐ์ฐ์๋ฅผ ํ์ฉํ์ฌ ํ์ ์ถ๋ก ์ ์ ์ฐ์ฑ์ ์ ์งํ๋ฉด์๋ ์๊ฒฉํ ํ์ ์์ ์ฑ์ ํ๋ณดํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์ธ์. ์ค์ฉ์ ์ธ ์์๋ฅผ ํตํด ์ค์ ํ๋ก์ ํธ์ ์ ์ฉํ๋ ๋ ธํ์ฐ๋ฅผ ๊ณต์ ํฉ๋๋ค.
- ๋จ์ด: 1,207๊ฐ15๋ถ
[๐ค] React 19 ์๋ก์ด ๊ธฐ๋ฅ: use ํ , Actions, ๊ทธ๋ฆฌ๊ณ ์ปดํ์ผ๋ฌ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
React 19์ ํต์ฌ ๋ณ๊ฒฝ ์ฌํญ์ธ use ํ , ์๋ฒ ์ก์ , ๊ทธ๋ฆฌ๊ณ React ์ปดํ์ผ๋ฌ์ ๋์ ๋ฐฐ๊ฒฝ๊ณผ ์ค์ ํ์ฉ ์์๋ฅผ ์ด์ค๊ธ ๊ฐ๋ฐ์ ๋๋์ด์ ๋ง์ถฐ ์์ธํ ์ค๋ช ํฉ๋๋ค. ์ต์ React ์ ๋ฐ์ดํธ๋ฅผ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ฑ๋ฅ๊ณผ ๊ฐ๋ฐ ๊ฒฝํ์ ํฅ์์ํค๋ ๋ฐฉ๋ฒ์ ์์๋ณด์ธ์.
- ๋จ์ด: 1,512๊ฐ16๋ถ
[๐ค] Next.js App Router ์บ์ฑ ์ ๋ต: ๋ฐ์ดํฐ ์ฌ๊ฒ์ฆ (revalidatePath, revalidateTag) ์๋ฒฝ ๊ฐ์ด๋
Next.js 14 App Router์์ ํจ์จ์ ์ธ ๋ฐ์ดํฐ ์บ์ฑ ์ ๋ต๊ณผ revalidatePath, revalidateTag๋ฅผ ์ด์ฉํ ๋ฐ์ดํฐ ์ฌ๊ฒ์ฆ ๋ฐฉ๋ฒ์ ์ค๋ฌด ์์์ ํจ๊ป ์์ธํ ์์๋ณด๊ณ ์น ์ฑ๋ฅ์ ์ต์ ํํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋ณด์ธ์.