Skip to content
GitHub

Required<T>

Required<T>: Properties အားလုံးကို မဖြစ်မနေ (Required) ဖြစ်အောင် လုပ်ခြင်း

Section titled “Required<T>: Properties အားလုံးကို မဖြစ်မနေ (Required) ဖြစ်အောင် လုပ်ခြင်း”
  • ဘာလုပ်ပေးသလဲ: Required<T> ဟာ Partial<T> ရဲ့ ပြောင်းပြန်ပါပဲ။ သူက T Type မှာရှိတဲ့ Properties အားလုံးကို မဖြစ်မနေ ထည့်သွင်းပေးရမယ့် (Required) Properties တွေ ဖြစ်သွားအောင် ပြောင်းလဲပြီး Type အသစ်တစ်ခု ဖန်တီးပေးပါတယ်။ မူရင်း Type မှာက Optional (? သင်္ကေတ ပါတာ) ဖြစ်နေရင်တောင်မှ၊ အဲ့ဒီ ? ကို ဖြုတ်ပစ်ပြီး မဖြစ်မနေ ထည့်ရမယ့် အရာအဖြစ် ပြောင်းလဲပစ်လိုက်ပါတယ်။
  • ဘယ်လို သုံးမလဲ: Required<UserProfile> လို့ ရေးရုံပါပဲ။
  • ရလာမယ့် Type (ပုံစံ): (မျက်စိထဲမှာ အောက်ကပုံစံမျိုး မြင်ယောင်ကြည့်ပါ)
index.ts
type RequiredUserProfile = {
id: number; // မဖြစ်မနေ ပါရမယ်
username: string; // မဖြစ်မနေ ပါရမယ်
email: string; // မူရင်း Type မှာက ? ပါပေမယ့် အခုတော့ မဖြစ်မနေ ပါရမယ်လို့ ပြောင်းသွားပြီ!
bio: string | null; // null ဖြစ်တာကို လက်ခံသေးတယ်၊ ဒါပေမယ့် bio ဆိုတဲ့ Property ကိုယ်တိုင်ကတော့ မဖြစ်မနေ ထည့်ပေးရမယ်
isActive: boolean; // မဖြစ်မနေ ပါရမယ်
}
  • ဘယ်လိုနေရာမှာ သုံးသလဲ: User က Data ဖြည့်တဲ့ Form တွေကနေ ရလာတဲ့ Object တစ်ခုဟာ အချက်အလက် တချို့ပဲ ပါချင်မှ ပါပါလိမ့်မယ်။ ဒါပေမဲ့ အဲ့ဒီ Object ကို လက်ခံပြီး အလုပ်လုပ်မယ့် အရေးကြီး Function တစ်ခုကတော့ Fields (အချက်အလက်) အားလုံး အပြည့်အစုံ ရှိနေမှသာ အလုပ်လုပ်နိုင်မယ် ဆိုတဲ့ အခြေအနေမျိုးတွေမှာ သုံးနိုင်ပါတယ်။
index.ts
// ဒီ Function က UserProfile အပြည့်အစုံ လိုအပ်တဲ့ နေရာမျိုးပါ။ (Optional လုံးဝ လက်မခံပါဘူး)
function processCompleteProfile(profile: Required<UserProfile>) {
// profile.email က ရှိ/မရှိ စစ်ဆေးနေစရာ မလိုဘဲ စိတ်ချလက်ချ ခေါ်သုံးလို့ ရသွားပါပြီ။
// ဘာလို့လဲဆိုတော့ Required ဖြစ်နေလို့ သေချာပေါက် ပါလာမယ်ဆိုတာ TypeScript က အာမခံထားလို့ပါ။
console.log(`Processing complete profile for: ${profile.email.toLowerCase()}`);
}
// မူရင်း userProfile Type အတိုင်း ရေးထားပြီး email မထည့်ပေးထားတဲ့ Object
// const incompleteProfile: UserProfile = { id: 1, username: "test", bio: null, isActive: true };
// processCompleteProfile(incompleteProfile); // TypeScript က Error ပြပါလိမ့်မယ်။ Required<UserProfile> ဖြစ်လို့ email က မဖြစ်မနေ လိုအပ်နေလို့ပါ!
// Required<UserProfile> Type အတိုင်း အချက်အလက် အားလုံး (email အပါအဝင်) အပြည့်အစုံ ရေးထားတဲ့ Object
const completeProfile: Required<UserProfile> = {
id: 1, username: "test", email: "test@example.com", bio: "A full bio.", isActive: true
};
processCompleteProfile(completeProfile); // အဆင်ပြေပြေ အလုပ်လုပ်ပါတယ်