Exclude<T,U>, Extract<T,U>, NonNullable<T>
ဒီ Utility Types ဥပမာတွေအတွက် အသုံးပြုမယ့် Union Type လေးတစ်ခုကို အရင်ဆုံး သတ်မှတ်ထားရအောင်။
type Status = "pending" | "processing" | "success" | "error" | null | undefined;Exclude<T, U>: Union ထဲက Types တွေကို ဖယ်ရှားခြင်း
Section titled “Exclude<T, U>: Union ထဲက Types တွေကို ဖယ်ရှားခြင်း”- ဘာလုပ်ပေးသလဲ:
Exclude<T, U>ဟာTဆိုတဲ့ Union Type ထဲကနေ၊Uမှာ သတ်မှတ်ထားတဲ့ Types တွေနဲ့ တူညီတဲ့ Types တွေကို ဖယ်ရှား (Exclude) ပစ်ပြီး ကျန်တဲ့ အရာတွေနဲ့ Type အသစ်တစ်ခု ဖန်တီးပေးပါတယ်။ (Omitနဲ့ သဘောတရား တူပေမဲ့ Union တွေအတွက်ပါ) - ဘယ်လို သုံးမလဲ:
Exclude<Status, "processing" | "pending"> - ရလာမယ့် Type:
"success" | "error" | null | undefined - ဘယ်အခါသုံးမလဲ: အခြေအနေ (Status) Type တွေ အများကြီး ရှိနေတယ်။ ဒါပေမဲ့ တချို့ အခြေအနေတွေကိုပဲ သီးသန့် ဂရုစိုက်ချင်ပြီး ကျန်တဲ့ မလိုအပ်တဲ့ အခြေအနေတွေကို ဖယ်ထုတ်ပစ်ချင်တဲ့အခါမျိုးမှာ သုံးနိုင်ပါတယ်။
// Status ထဲကနေ "pending" နဲ့ "processing" ကို ဖယ်ထုတ် (Exclude) လိုက်တယ်type FinalStatus = Exclude<Status, "pending" | "processing">;
let final: FinalStatus = "success"; // ဒါက အဆင်ပြေပြေ အလုပ်လုပ်တယ်
// final = "pending"; // Error ပြပါလိမ့်မယ်။ 'pending' ဆိုတာ FinalStatus Type ထဲမှာ မပါဝင်တော့လို့ပါ!Extract<T, U>: Union ထဲက Types တွေကို ရွေးချယ်ခြင်း
Section titled “Extract<T, U>: Union ထဲက Types တွေကို ရွေးချယ်ခြင်း”- ဘာလုပ်ပေးသလဲ:
Extract<T, U>ဟာTဆိုတဲ့ Union Type ထဲကနေ၊Uမှာ သတ်မှတ်ထားတဲ့ Types တွေနဲ့ တူညီတဲ့ Types တွေကိုသာ သီးသန့် ရွေးချယ် (Extract) ထုတ်ယူပြီး Type အသစ်တစ်ခု ဖန်တီးပေးပါတယ်။ (Pickနဲ့ သဘောတရား တူပေမဲ့ Union တွေအတွက်ပါ) - ဘယ်လို သုံးမလဲ:
Extract<Status, "success" | "error"> - ရလာမယ့် Type:
"success" | "error" - ဘယ်အခါသုံးမလဲ: ကျယ်ပြန့်တဲ့ Union Type ကြီး တစ်ခု ရှိနေတယ်။ ဒါပေမဲ့ အဲ့ဒီထဲကမှ ကိုယ်လိုအပ်တဲ့ အစိတ်အပိုင်း တချို့ကိုပဲ သီးသန့် ခွဲထုတ်ပြီး အသုံးပြုချင်တဲ့ အခါမျိုးမှာ သုံးနိုင်ပါတယ်။
// Status ထဲကနေ "success" နဲ့ "error" ကိုပဲ သီးသန့် ရွေးထုတ် (Extract) လိုက်တယ်type ResolvedStatus = Extract<Status, "success" | "error">;
let resolved: ResolvedStatus = "error"; // ဒါက အလုပ်လုပ်တယ်
// resolved = "pending"; // Error ပြပါလိမ့်မယ်။// resolved = null; // Error ပြပါလိမ့်မယ်။NonNullable<T>: null နှင့် undefined ကို ဖယ်ရှားခြင်း
Section titled “NonNullable<T>: null နှင့် undefined ကို ဖယ်ရှားခြင်း”- ဘာလုပ်ပေးသလဲ:
NonNullable<T>ဟာTဆိုတဲ့ Type ထဲမှာnullဒါမှမဟုတ်undefinedဆိုတဲ့ တန်ဖိုးတွေ ပါဝင်နေရင်၊ အဲ့ဒီအရာတွေကို ဖယ်ရှား (Exclude) ပစ်ပြီး ကျန်တဲ့ အစစ်အမှန် တန်ဖိုးတွေနဲ့ Type အသစ်တစ်ခု ဖန်တီးပေးပါတယ်။ - ဘယ်လို သုံးမလဲ:
NonNullable<Status> - ရလာမယ့် Type:
"pending" | "processing" | "success" | "error" - ဘယ်အခါသုံးမလဲ: Variable တစ်ခုရဲ့ တန်ဖိုးက
nullဒါမှမဟုတ်undefinedဖြစ်နိုင်ခြေ ရှိနေတယ်။ ဒါပေမဲ့ Code ထဲမှာ if statement တွေနဲ့ သေချာ စစ်ဆေး (Check) ပြီးသွားတဲ့ နောက်ပိုင်းမှာတော့၊ အဲ့ဒီ တန်ဖိုးဟာnullဒါမှမဟုတ်undefinedလုံးဝ မဟုတ်တော့ဘူးလို့ သေချာသွားတဲ့ အခါမျိုးမှာ Type ကို ပြောင်းလဲဖို့အတွက် အရမ်း အသုံးဝင်ပါတယ်။
let currentStatus: Status = "pending";
// ... တခြား လုပ်ဆောင်ချက် (Logic) တွေ ...
// currentStatus က သေချာပေါက် ရှိနေတယ်လို့ စစ်ဆေးလိုက်တာif (currentStatus !== null && currentStatus !== undefined) { // ဒီ if block ထဲမှာဆိုရင် TypeScript က currentStatus ဟာ null ဒါမှမဟုတ် undefined လုံးဝ မဟုတ်တော့ဘူးဆိုတာကို အလိုအလျောက် သိရှိနေပါပြီ။ // ဒါကြောင့် တခြား Function တစ်ခုကို ပို့တဲ့အခါ Type ကို အတိအကျ သတ်မှတ်ပေးလို့ ရပါတယ်:
let activeStatus: NonNullable<Status> = currentStatus; console.log(activeStatus.toUpperCase()); // null ဖြစ်မနေတာ သေချာသွားပြီမို့ toUpperCase() ကို စိတ်ချယုံကြည်စွာ ခေါ်သုံးလို့ ရသွားပါပြီ။}