ရိုးရိုးလေး ရှင်းပြမယ်
Conditional type နဲ့ infer ကို ရိုးရိုးလေး ပြောရရင်
Conditional type ဆိုတာ value တွေအစား type တွေပေါ်မှာ run တဲ့ if/else ပါ။ 'ဒီ type က Promise ဖြစ်ရင် တစ်ခု ပေး၊ မဖြစ်ရင် နောက်တစ်ခု ပေး' လို့ ပြောတာပါ။ infer ကတော့ အဲဒီ မေးခွန်းထဲမှာ တွဲပါလာတဲ့ ဖမ်းယူကိရိယာပါ: 'ဒါက Promise ဖြစ်ရင် အထဲမှာ ဘာရှိလဲ ပြောပြ' ပေါ့။
Parcel ထုပ်ကို ဖောက်တာနဲ့ တူပါတယ်။ Promise<User> က User ကို ထည့်ထားတဲ့ box ပါ။ Conditional type က box ဖြစ်မဖြစ် စစ်ပြီး infer က အထဲက item ကို ထုတ်ပေးပါတယ်၊ ဒါကြောင့် type ကို လက်နဲ့ ပြန်မရေးဘဲ User ကို ရပါတယ်။ အားလုံးက compile time မှာပဲ ဖြစ်ပြီး run တဲ့ code ထဲကို ဘာမှ မရောက်ပါဘူး။
Problem → Why → Solution
ပြဿနာ: wrapper ထဲက type ကို လိုချင်တယ်
async function getProfile() တစ်ခု ရှိပြီး React component တစ်ခုက prop အဖြစ် လက်ခံနိုင်ဖို့ အဲဒါ resolve ဖြစ်တဲ့ data ရဲ့ type ကို လိုချင်ပါတယ်။ အဲဒီ type ကို လက်နဲ့ ရေးရင် function ရဲ့ shape ကို ထပ်ရေးသလို ဖြစ်ပြီး function ပြောင်းတာနဲ့ ကွဲထွက်သွားပါတယ်။ ReturnType<typeof getProfile> က Promise<Profile> ကို ပေးပါတယ်၊ ဒါက wrapper ပါ၊ data မဟုတ်ပါဘူး။ Type ထဲကို ကြည့်ပြီး အစိတ်အပိုင်းတစ်ခုကို ဆွဲထုတ်နိုင်တဲ့ နည်းလမ်း လိုပါတယ်။
async function getProfile() {
return { name: "Ada", followers: 1200 };
}
// Hand-copied: drifts when getProfile changes
type Profile = { name: string; followers: number };
type Wrong = ReturnType<typeof getProfile>; // Promise<...>, not the data
function profileTitle(profile: Wrong) {
return profile.name; // error: 'name' does not exist on type 'Promise<...>'
}ဘာကြောင့် ခက်တာလဲ
Promise<T>, T[] နဲ့ function type တွေက သင် တကယ် ဂရုစိုက်တဲ့ type ကို ထုပ်ထားပါတယ်။ Pattern တစ်ခုနဲ့ တိုက်စစ်ပြီး အတွင်းပိုင်းကို ဆွဲထုတ်နိုင်တဲ့ နည်းလမ်း မရှိရင် type တွေကို လက်နဲ့ ထပ်ခါထပ်ခါ ရေးနေရပါမယ်။ infer ပါတဲ့ conditional type တွေက အဲဒီ pattern match ပဲ ဖြစ်ပြီး ReturnType, Parameters, Awaited တို့ကိုလည်း အဲဒီနည်းနဲ့ ဆောက်ထားတာပါ။
Core concept: type ပေါ်က if/else နဲ့ pattern matching
Conditional type ရဲ့ ပုံစံက T extends U ? X : Y ပါ။ T က U ထဲကို assign လုပ်လို့ ရရင် ရလဒ်က X ဖြစ်ပြီး မရရင် Y ဖြစ်ပါတယ်။ ဒီမှာ extends ဆိုတာ generic constraint မှာလိုပဲ 'ထဲကို ဝင်ဆံ့တယ်' လို့ အဓိပ္ပာယ်ရပါတယ်: string extends string | number က true ဖြစ်ပြီး { id: string; name: string } extends { id: string } ကလည်း true ပါ၊ ပိုပါတဲ့ property တွေကို ခွင့်ပြုလို့ပါ။ Conditional type တွေကို type parameter နဲ့ တွဲရေးလေ့ရှိတဲ့အတွက် ထည့်ပေးတဲ့ type တိုင်းအတွက် ဆုံးဖြတ်ချက်ကို အသစ် ပြန်ချပါတယ်။
infer က တိုက်စစ်နေတဲ့ type ရဲ့ အစိတ်အပိုင်းတစ်ခုကို ဖမ်းယူခွင့် ပေးပါတယ်။ T extends Promise<infer Value> ? Value : T မှာ TypeScript က T ကို တစ်ခုခုရဲ့ Promise အဖြစ် မြင်ကြည့်ပါတယ်၊ အောင်မြင်ရင် အဲဒီ တစ်ခုခုကို Value နဲ့ ချိတ်ပေးပြီး Value ကို true branch ထဲမှာပဲ သုံးလို့ ရပါတယ်။ infer ကို type pattern ထဲ ဘယ်နေရာမှာမဆို သုံးလို့ ရပါတယ်: array ရဲ့ element ((infer Item)[])၊ function ရဲ့ return type ((...args: any[]) => infer R) ဒါမှမဟုတ် parameter တွေ။
စစ်နေတဲ့ type က ဗလာ type parameter ဖြစ်ပြီး union တစ်ခု ပေးလိုက်ရင် condition ကို member တစ်ခုချင်းစီပေါ် သီးခြား apply လုပ်ပြီး ရလဒ်တွေကို ပြန်ပေါင်းပါတယ်။ ဒါကို distribution လို့ ခေါ်ပါတယ်။ type ToArray<T> = T extends unknown ? T[] : never က string | number ကို (string | number)[] မဟုတ်ဘဲ string[] | number[] အဖြစ် ပြောင်းပါတယ်။ Exclude<T, U> က union ထဲက member တွေကို ဖယ်ထုတ်တာလည်း distribution ကြောင့်ပါ။ ပိတ်ချင်ရင် ဘက်နှစ်ဖက်လုံးကို tuple နဲ့ ထုပ်ပါ: [T] extends [U]။
| Built-in | လုပ်ဆောင်ချက် |
|---|---|
| NonNullable<T> | null နဲ့ undefined ကို ဖယ် |
| Exclude<T, U> / Extract<T, U> | U ထဲ ဝင်ဆံ့တဲ့ member တွေကို ဖယ် / ချန် |
| ReturnType<F> / Parameters<F> | function ရဲ့ return type / parameter tuple |
| Awaited<T> | Promise ရဲ့ resolve value ကို အဆင့်ဆင့် ဖြေထုတ် |
အချိန်အများစုမှာ ကိုယ်ပိုင် ရေးမယ့်အစား ဒီ built-in တွေကို သုံးပါ။ Awaited<T> (TypeScript 4.5 ကတည်းက ပါလာတာ) က ဒီ lesson ရဲ့ AwaitedResult ရဲ့ production အဆင့် version ပါ — nested promise တွေနဲ့ တခြား thenable တွေကိုပါ ဖြေထုတ်ပေးပါတယ်။ ဒါတွေအားလုံး compile time မှာပဲ ရှိတာကို မမေ့ပါနဲ့၊ conditional type က runtime မှာ value ကို စစ်လို့ မရတဲ့အတွက် typeof ဒါမှမဟုတ် schema လို တကယ့် check ကို ဘယ်တော့မှ အစားမထိုးနိုင်ပါဘူး။
- မေး: T က pattern နဲ့ ကိုက်လား?
- infer က လိုချင်တဲ့ အပိုင်းကို နာမည်ပေး
- ကိုက်ရင်: true branch ကို ပြန်ပေး
- မကိုက်ရင်: fallback ကို ပြန်ပေး
Union ဆိုရင် member တစ်ခုစီအတွက် တစ်ကြိမ်စီ မေးပြီး အဖြေတွေကို ပြန်ပေါင်းပါတယ်။
အထွေထွေ ပုံစံ
type Check<T> = T extends Pattern ? WhenYes : WhenNo;
type Unwrap<T> = T extends Wrapper<infer Inner> ? Inner : T;
type ResultOf<F> = F extends (...args: any[]) => infer R ? R : never;
type NoDistribute<T> = [T] extends [Pattern] ? WhenYes : WhenNo;Condition က assignability ကို စစ်ပါတယ်။ infer က pattern ထဲက နေရာတစ်ခုကို နာမည်ပေးပြီး true branch မှာ သုံးခွင့်ပေးပါတယ်။ Union ကို တစ်ခုလုံးအဖြစ် စစ်ချင်ရင် [T] နဲ့ ထုပ်ပါ။
Worked example: Promise ကို ဖြေထုတ်ခြင်း
AwaitedResult<T> က Promise ဆိုရင် အထဲက value type ကို ပြန်ပေးပြီး မဟုတ်ရင် T ကို မပြောင်းဘဲ ပြန်ပေးပါတယ်။ ReturnType နဲ့ ပေါင်းလိုက်တဲ့အခါ async function ကနေ Task type ကို ထပ်မရေးဘဲ derive လုပ်ပေးပါတယ်။
type AwaitedResult<T> = T extends Promise<infer Value> ? Value : T;
type ApiResult = AwaitedResult<Promise<{ id: string }>>; // { id: string }
type PlainResult = AwaitedResult<number>; // number (not a Promise)
async function fetchTask() {
return { id: "task-1", done: false };
}
// Derive the resolved type from the function instead of writing it again
type Task = AwaitedResult<ReturnType<typeof fetchTask>>;
async function main() {
const task: Task = await fetchTask();
const item: ApiResult = { id: task.id };
const count: PlainResult = 3;
console.log(item.id, task.done, count);
}
main();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The condition
type AwaitedResult<T> = T extends Promise<infer Value> ? Value : T;T ကို Promise<something> အဖြစ် ကိုက်ညီအောင် ကြိုးစားပါတယ်။ ကိုက်ရင် something ကို Value အဖြစ် ဖမ်းပြီး ပြန်ပေး၊ မကိုက်ရင် T ကိုယ်တိုင်ကို ပြန်ပေးပါတယ်။
- Promise branch
type ApiResult = AwaitedResult<Promise<{ id: string }>>; // { id: string }Promise<{ id: string }> က pattern နဲ့ ကိုက်တဲ့အတွက် Value က { id: string } ဖြစ်ပါတယ်။
- Fallback branch
type PlainResult = AwaitedResult<number>; // number (not a Promise)number က Promise မဟုတ်လို့ false branch ကို ရွေးပြီး T = number ကို မပြောင်းဘဲ ပြန်ပေးပါတယ်။
- Derive from a function
type Task = AwaitedResult<ReturnType<typeof fetchTask>>;typeof fetchTask က function type ကို ယူ၊ ReturnType က Promise<{ id; done }> ကို ထုတ်၊ AwaitedResult က box ကို ဖောက်ပါတယ်။ fetchTask မှာ field အသစ် ထည့်ရင် Task က အလိုအလျောက် လိုက်ပြောင်းပါတယ်။
task-1 false 3Union ပေါ်မှာ distribution
အောက်က type နှစ်ခုက တစ်ခုတည်းပဲ ကွာပါတယ်: ဒုတိယ တစ်ခုက T ကို tuple ထဲ ထုပ်ထားပါတယ်။ ရလဒ်တွေနဲ့ comment ထားတဲ့ error ကို နှိုင်းယှဉ်ကြည့်ပါ။
type ToArray<T> = T extends unknown ? T[] : never;
type ToArrayAll<T> = [T] extends [unknown] ? T[] : never;
type A = ToArray<string | number>; // string[] | number[]
type B = ToArrayAll<string | number>; // (string | number)[]
type Status = "idle" | "loading" | "success" | "error";
type Settled = Exclude<Status, "idle" | "loading">; // "success" | "error"
const a: A = ["x", "y"]; // fine: a string[]
// const bad: A = ["x", 1]; // error: a mixed array is neither member
const b: B = ["x", 1]; // fine: one mixed array
const settled: Settled[] = ["success", "error"];
console.log(a, b, settled);Exclude ပြသလို union တွေကို filter လုပ်တဲ့အခါ distribution က များသောအားဖြင့် လိုချင်တာပါ။ IsString<T> လို check တစ်ခု ရေးပြီး union တစ်ခု ပေးလိုက်တဲ့အခါမှာတော့ လူတွေကို အံ့ဩစေပါတယ်: member တစ်ခုချင်းစီကို သီးခြား စစ်လို့ အဖြေက "yes" | "no" အဖြစ် ပြန်လာပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ conditional type များ
Application code မှာ နက်ရှိုင်းတဲ့ conditional type တွေကို ရေးခဲပေမဲ့ standard library နဲ့ framework တွေကနေတစ်ဆင့် အမြဲ သုံးနေရပါတယ်:
- Function ကနေ type ကို derive လုပ်ခြင်း: Awaited<ReturnType<typeof loadDashboard>> က component ကို data shape အတိအကျကို ထပ်မရေးဘဲ ပေးပြီး Parameters<typeof sendEmail>[0] က function ရဲ့ options object ကို ပြန်သုံးပါတယ်။
- Union တွေကို သန့်ရှင်းခြင်း: guard တစ်ခုပြီးနောက် NonNullable<User | null>၊ ဒါမှမဟုတ် public API ကနေ event တွေကို ဖုံးဖို့ Exclude<AppEvent, { type: "internal" }>။
- Library type များ: typed router အချို့က "/users/:id" လို route string တွေကို conditional type နဲ့ infer သုံးပြီး ခွဲခြမ်းကာ typed params.id ကို ပေးပါတယ်။
- Helper တွေကို ရိုးသားအောင် ထားခြင်း: withRetry<F extends (...args: any[]) => Promise<unknown>>(fn: F) လို typed wrapper က Parameters<F> နဲ့ ReturnType<F> တူတဲ့ function ကို ပြန်ပေးနိုင်လို့ ခေါ်သူတွေက မူရင်း signature ကို မြင်ရပါတယ်။
အမှားများ
Conditional type တွေမှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Union က distribute ဖြစ်တာကို မေ့ခြင်း
IsString<string | number> က "no" မဟုတ်ဘဲ "yes" | "no" ဖြစ်ပါတယ်။ 'type တစ်ခုလုံးက string လား' လို့ မေးချင်တာဆိုရင် ဘက်နှစ်ဖက်လုံးကို ထုပ်ပါ: [T] extends [string]။
Runtime effect ရှိမယ်လို့ မျှော်လင့်ခြင်း
Conditional type က value ကို ကြည့်လို့ မရပါဘူး၊ type IsEmail<T> တစ်ခုက email ကို ဘယ်တော့မှ validate မလုပ်ပေးပါဘူး။ Code မ run ခင် type တွေ ဖျက်ခံရလို့ boundary တွေမှာ typeof, type guard ဒါမှမဟုတ် schema လို တကယ့် check တွေနဲ့ တွဲသုံးပါ။
ဖတ်မရတဲ့ type ပဟေဠိ ဆောက်ခြင်း
အထပ်ထပ် nested ဖြစ်နေတဲ့ conditional type တွေက ဘယ်သူမှ ဖတ်မရတဲ့ error message တွေ ထုတ်ပေးပြီး compiler ကိုလည်း နှေးစေပါတယ်။ Built-in (Awaited, ReturnType, Parameters) တွေကို ဦးစားပေးပါ၊ အလယ်အဆင့်တွေကို နာမည်ပေးပါ၊ ရိုးရိုး interface က ပိုရှင်းမယ်ဆိုရင် ရပ်လိုက်ပါ။
Best Practices
Best practices
- Type ကို ထပ်မရေးခင် ရှိပြီးသား function ဒါမှမဟုတ် value ကနေ typeof နဲ့ built-in တွေသုံးပြီး derive လုပ်လို့ ရမလား အရင် မေးပါ။
- Conditional type တစ်ခုကို ရေးပြီးတိုင်း union, never နဲ့ any လို အစွန်းရောက် input တွေနဲ့ စမ်းကြည့်ပါ၊ distribution ကြောင့် ရလဒ် မထင်မှတ်ဘဲ ပြောင်းနိုင်ပါတယ်။
- ရှုပ်ထွေးတဲ့ type တစ်ခုကို အသုံးပြုပုံ ဥပမာ တစ်ကြောင်းနဲ့ comment ရေးထားပါ၊ ဒါမှ နောက်ဖတ်မယ့်သူ hover လုပ်စရာ မလိုပါဘူး။
Mini challenge
Mini challenge: ElementType နဲ့ FirstParam
Extractor type နှစ်ခု ရေးပြီး event system လေးတစ်ခုကို သူ့ handler function နဲ့ ကိုက်ညီနေအောင် သုံးပါ။ ElementType<T> က array ရဲ့ item type ကို ပေးပြီး array မဟုတ်ရင် T ကိုယ်တိုင်ကို ပေးပါတယ်။ FirstParam<F> က function ရဲ့ ပထမ parameter type ကို ပေးပါတယ်။
- ElementType<T> က array pattern ပေါ်မှာ infer သုံးပြီး T ကို fallback အဖြစ် ပြန်ပေးရမယ်
- FirstParam<F> က function pattern ပေါ်မှာ infer သုံးပြီး never ကို fallback အဖြစ် ပြန်ပေးရမယ်
- dispatch ရဲ့ payload ကို ပြန်မရေးဘဲ FirstParam<typeof onSignup> နဲ့ type သတ်မှတ်ပါ
- Queue ထဲက event တစ်ခုစီအတွက် message တစ်ကြောင်းစီ print ထုတ်ပါ
// TODO: the item type of an array, or T itself
type ElementType<T> = unknown;
// TODO: the type of a function's first parameter, or never
type FirstParam<F> = unknown;
function onSignup(event: { email: string; plan: "free" | "pro" }) {
return `Welcome ${event.email} (${event.plan})`;
}
// TODO: type payload with FirstParam<typeof onSignup>
function dispatch(payload) {
// TODO: hand the payload to onSignup and return its message
}
// TODO: build a queue of events, add one more typed as ElementType<typeof queue>,
// and print dispatch(event) for each of themရလဒ်: Event တစ်ခုစီအတွက် Welcome ada@example.com (pro) လို message တစ်ကြောင်းစီ print ထုတ်ပါတယ်။ plan: "gold" ပါတဲ့ payload က compile မဖြစ်ပါဘူး၊ ဒါက FirstParam က onSignup ရဲ့ type အတိအကျကို ယူထားလို့ပါ။
အရိပ်အမြွက် ကြည့်ရန်
Array အတွက် pattern က T extends readonly (infer Item)[] ? Item : T ဖြစ်ပါတယ်။ Function အတွက်တော့ (first: infer P, ...rest: any[]) => any ကို pattern အဖြစ် သုံးပါ။
အဖြေ ကြည့်ရန်
type ElementType<T> = T extends readonly (infer Item)[] ? Item : T;
type FirstParam<F> = F extends (first: infer P, ...rest: any[]) => any ? P : never;
function onSignup(event: { email: string; plan: "free" | "pro" }) {
return `Welcome ${event.email} (${event.plan})`;
}
type SignupEvent = FirstParam<typeof onSignup>; // { email: string; plan: ... }
function dispatch(payload: SignupEvent): string {
return onSignup(payload);
}
const queue: SignupEvent[] = [
{ email: "ada@example.com", plan: "pro" },
{ email: "lin@example.com", plan: "free" },
];
type QueueItem = ElementType<typeof queue>; // SignupEvent
const late: QueueItem = { email: "grace@example.com", plan: "free" };
for (const event of [...queue, late]) console.log(dispatch(event));
// dispatch({ email: "x@example.com", plan: "gold" }); // error: not a valid planQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- T extends U ? X : Y က type ပေါ်က if/else ဖြစ်ပြီး T က U ထဲ ဝင်ဆံ့ရင် X၊ မဆံ့ရင် Y ကို ရွေးပါတယ်။
- infer က pattern ထဲက အစိတ်အပိုင်းတစ်ခုကို ဖမ်းပြီး true branch မှာ သုံးခွင့်ပေးပါတယ်။
- ဗလာ type parameter ပေါ်က condition က union member တစ်ခုချင်းစီကို distribute ဖြစ်ပါတယ်၊ [T] နဲ့ ပိတ်ပါ။
- ကိုယ်ပိုင် မရေးခင် Awaited, ReturnType, Parameters, Exclude, NonNullable ကို သုံးပါ၊ runtime check ကို ဘယ်တော့မှ အစားမထိုးပါဘူး။
Conditional Types — TypeScript