Skip to content
GitHub

Partial<T>

ဒီ Utility Type ဥပမာတွေ အကုန်လုံးအတွက် အခြေခံ အသုံးပြုမယ့် UserProfile Type လေးတစ်ခုကို အရင်ဆုံး သတ်မှတ်ထားရအောင်။

index.ts
interface UserProfile {
id: number;
username: string;
email?: string; // email က Optional (ပါချင်မှပါ၊ မပါလည်းရ) ဖြစ်အောင် ? လေး တပ်ထားတယ်
bio: string | null; // bio ကတော့ string ဒါမှမဟုတ် null နှစ်ခုထဲက တစ်ခုခု ဖြစ်နိုင်တယ်
isActive: boolean;
}

Partial<T>: Properties အားလုံးကို Optional ဖြစ်အောင် လုပ်ခြင်း

Section titled “Partial<T>: Properties အားလုံးကို Optional ဖြစ်အောင် လုပ်ခြင်း”
  • ဘာလုပ်ပေးသလဲ: Partial<T> ဟာ T Type (မူရင်း Type) မှာရှိတဲ့ Properties အားလုံးကို Optional (ပါချင်မှပါ၊ ထည့်ချင်မှ ထည့်) အဖြစ် ပြောင်းလဲပေးပြီး Type အသစ်တစ်ခု အလိုအလျောက် ဖန်တီးပေးပါတယ်။
  • ဘယ်လို သုံးမလဲ: Partial<UserProfile> လို့ ရေးရုံပါပဲ။
  • ရလာမယ့် Type (ပုံစံ): (မျက်စိထဲမှာ အောက်ကပုံစံမျိုး မြင်ယောင်ကြည့်ပါ)
index.ts
interface PartialUserProfile {
id?: number; // id က Optional ဖြစ်သွားပြီ
username?: string; // username က Optional ဖြစ်သွားပြီ
email?: string; // email က မူလကတည်းက Optional ပါပဲ
bio?: string | null; // bio က Optional ဖြစ်သွားပြီ
isActive?: boolean; // isActive က Optional ဖြစ်သွားပြီ
}
  • ဘယ်လိုနေရာမှာ သုံးသလဲ: User ရဲ့ အချက်အလက်တွေကို အကုန်လုံး မဟုတ်ဘဲ တစ်စိတ်တစ်ပိုင်းပဲ ပြင်ဆင် (Update) တဲ့ Function မျိုးတွေအတွက် အရမ်း အသုံးဝင်ပါတယ်။ မလိုအပ်တဲ့ Fields တွေကို ချန်ထားခဲ့ပြီး၊ ကိုယ် ပြင်ဆင်ချင်တဲ့ Field (အချက်အလက်) လေးတွေကိုပဲ ရွေးချယ် ပို့ပေးလို့ ရသွားတာပေါ့။
index.ts
// ဒီ Function က user ကို ပြင်ဆင် (Update) ဖို့အတွက်ပါ။
// အချက်အလက် အကုန်လုံး မလိုဘူး၊ ပြင်ချင်တဲ့ အချက်အလက်လေးတွေပဲ ပို့ပေးဖို့ Partial<UserProfile> ကို သုံးထားတယ်
function updateUser(userId: number, updates: Partial<UserProfile>) {
// ... Database ထဲက user ကို ရှာပြီး updates တွေကို အစားထိုး ထည့်သွင်းမယ့် လုပ်ငန်းစဉ် (Logic) ...
console.log(`Updating user ${userId} with:`, updates);
}
// userId 1 ကို username တစ်ခုတည်း ပြင်ဖို့ ပို့လိုက်တယ် (အခြားအချက်အလက်တွေ ပို့စရာမလိုဘူး)
updateUser(1, { username: "new_username" });
// userId 2 ကို email နဲ့ isActive နှစ်ခုကို ပြင်ဖို့ ပို့လိုက်တယ်
updateUser(2, { email: "new@example.com", isActive: false });
// updateUser(3, { unknownProperty: "test" }); // Error ပြပါလိမ့်မယ်။ unknownProperty ဆိုတာ UserProfile ထဲမှာ လုံးဝ မရှိလို့ပါ!