အားလုံးကို ပေါင်းစပ်အသုံးပြုခြင်း: ပူးပေါင်းဆောင်ရွက်ခြင်း!
Generics နဲ့ Utility Types တွေကို ပေါင်းစပ်အသုံးပြုခြင်း
Section titled “Generics နဲ့ Utility Types တွေကို ပေါင်းစပ်အသုံးပြုခြင်း”Utility Types တွေဟာ Generic ဖြစ်ပါတယ်!
Section titled “Utility Types တွေဟာ Generic ဖြစ်ပါတယ်!”Partial\<T>
၊ Pick\<T, K>
၊ Readonly\<T>
လိုမျိုး Utility Types
တွေကို မှတ်မိသေးလား။ သူတို့မှာ ပါတဲ့ \<T>
နဲ့ \<K>
ဆိုတာတွေက ဘာတွေလဲဆိုတော့ Generic Type Parameters
(Type ရဲ့ နေရာမှာ အစားထိုးခံရမယ့် အမျိုးအစားကို ကိုယ်စားပြုတဲ့ အမည်) တွေပါပဲ။
Utility Types
အများစုက အခြေခံအားဖြင့် Generic Types
တွေပါပဲ။ သူတို့က Types တွေကို ပုံစံပြောင်းလဲပေးတဲ့ အလုပ်တွေကို လုပ်ဆောင်ပါတယ်။ TypeScript က ဒါတွေကို အသင့်ပေးထားတာဖြစ်ပေမယ့်၊ Generic
တွေရဲ့ သဘောတရားအတိုင်းပဲ အလုပ်လုပ်တာပါ။
ဥပမာအနေနဲ့၊ Partial\<T>
ရဲ့ ရိုးရှင်းတဲ့ ပုံစံကို ကြည့်ရအောင် (ဒါက Mapped Types
ကို အသုံးပြုထားတာပါ၊ နောက်မှ အသေးစိတ် လေ့လာပါမယ်)။
// Mapped Types ကို အကြမ်းဖျင်း ကြည့်ရှုခြင်း// type Partial<T> = {// [P in keyof T]?: T[P]; // T (Object) ထဲက property P (တစ်ခုချင်းစီ) တိုင်းကို optional ဖြစ်စေ (?:) ။// };
Generics
နဲ့ Utility
Types တွေကို သုံးပြီး ကိုယ်ပိုင် Function/Type တွေ ဖန်တီးခြင်း:
Section titled “Generics နဲ့ Utility Types တွေကို သုံးပြီး ကိုယ်ပိုင် Function/Type တွေ ဖန်တီးခြင်း:”Generics နဲ့ Utility Types တွေကို ပေါင်းစပ်ပြီး လက်တွေ့ ဥပမာတစ်ခု လုပ်ကြည့်ရအောင်။ object တစ်ခုနဲ့၊ အဲဒီ object ထဲက key တွေ (properties တွေရဲ့ နာမည်) ပါတဲ့ array တစ်ခုကို လက်ခံတဲ့ function တစ်ခုကို စဉ်းစားကြည့်ပါ။ ဒီ function ကနေ အဲဒီ key တွေနဲ့ ကိုက်ညီတဲ့ properties တွေကိုပဲ ရွေးထုတ်ပြီး object အသစ်တစ်ခု ပြန်ပေးမယ်။ ဒါပေမယ့် ပြန်ပေးတဲ့ properties တွေရဲ့ value တွေကိုတော့ string အဖြစ် ပြောင်းထားမယ် (ဒါမှမဟုတ် string ပြောင်းလို့မရရင် null ဖြစ်စေမယ်)။
function pickAndStringifyValues<T extends object, K extends keyof T>( // <T extends object, K extends keyof T> ဆိုတာ Generic ပါ။ obj: T, // obj က T ဆိုတဲ့ Type ဖြစ်ရမယ်။ keys: K[]): Record<K, string | null> { // ပြန်လာမယ့် Type မှာ Record utility type ကို သုံးထားတယ်။ const result = {} as Record<K, string | null>; // အစမှာတော့ 'as' ကိုသုံးပြီး ဗလာ object တစ်ခု ဖန်တီးပါ။ for (const key of keys) { // keys array ထဲက key တစ်ခုချင်းစီကို စစ်ဆေးမယ်။ if (obj.hasOwnProperty(key)) { // obj မှာ ဒီ key ပါလား စစ်မယ်။ const value = obj[key]; // key နဲ့ ကိုက်ညီတဲ့ value ကို ယူမယ်။ result[key] = value !== null && value !== undefined ? String(value) : null; // value ကို string ပြောင်း၊ မဟုတ်ရင် null ထား။ } } return result; // result object ကို ပြန်ပေး။}
const sampleUser = { name: "Alice", age: 30, isActive: true };const stringifiedInfo = pickAndStringifyValues(sampleUser, ["name", "age"]);
// typeof stringifiedInfo သည် Record<"name" | "age", string | null> ဖြစ်သည်။// stringifiedInfo = { name: "Alice", age: "30" }console.log(stringifiedInfo.name); // "Alice" (string)// console.log(stringifiedInfo.isActive); // Error: 'isActive' ဆိုတဲ့ property မရှိပါ။ (ဒီနေရာမှာ error ပြလိမ့်မယ်။)
Utility Types တွေကို ပေါင်းစပ်အသုံးပြုခြင်း:
Section titled “Utility Types တွေကို ပေါင်းစပ်အသုံးပြုခြင်း:”တစ်ခါတလေကျတော့ type ကို ပြောင်းလဲဖို့အတွက် Utility Types
အများကြီးကို ပေါင်းစပ်ပြီး သုံးဖို့ လိုအပ်တတ်ပါတယ်။
interface Product { id: string; name: string; price: number; description?: string; tags: string[];}
// product update အတွက် payload type တစ်ခု လိုချင်ပါတယ်။// - 'id' ကလွဲလို့ 'name', 'price', 'description', 'tags' တွေပဲ ပြောင်းလို့ရရမယ်။ (Omit 'id' ကို သုံး)// - ပြောင်းလို့ရတဲ့ field တွေ အားလုံးက optional ဖြစ်ရမယ်။ (Partial ကို သုံး)// - ဒီ object တစ်ခုလုံးက လက်ခံတဲ့ function အတွက် readonly ဖြစ်ရမယ်။ (Readonly ကို သုံး)
type ProductUpdatePayload = Readonly<Partial<Omit<Product, "id">>>;
// ဒါကို တစ်ဆင့်ချင်း ရှင်းပြပါမယ်။// 1. Omit<Product, "id"> -> { name: string; price: number; description?: string; tags: string[]; } (id ကို ဖယ်လိုက်)// 2. Partial<Result of 1> -> { name?: string; price?: number; description?: string; tags?: string[]; } (အားလုံးကို optional ဖြစ်စေ)// 3. Readonly<Result of 2> -> { readonly name?: string; readonly price?: number; ... } (အားလုံးကို ပြောင်းမရအောင် လုပ်)
function handleProductUpdate(payload: ProductUpdatePayload) {// payload.name = "New Name"; // Error! Readonly ဖြစ်နေလို့ပါ။if (payload.price !== undefined) {console.log("Price update:", payload.price);}}
handleProductUpdate({ name: "Super Gadget", price: 199.99 });handleProductUpdate({ description: "The best gadget ever." });