Thuta Learning
TypeScript
AdvancedProgrammingintermediate

Utility & Mapped Types

TypeScriptသင်ခန်းစာ 20

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Utility & Mapped Types ရဲ့ runtime value နဲ့ compile-time type ဆက်နွယ်မှုကိုရှင်းပြနိုင်ရန်
  • Strict mode တွင် unsafe input နဲ့ edge cases စမ်းနိုင်ရန်
  • Maintainable TypeScript contracts ရေးနိုင်ရန်

Utility & Mapped Types မှာ caller နဲ့ implementation ကြား contract ကို minimal ဖြစ်အောင်ရေးပါ။ Generic constraints, variance, readonly data နဲ့ structural compatibility ကိုစဉ်းစားပြီး type assertion ဖြင့်အမှန်တရားကိုဖုံးမထားပါနှင့်။

ပိုပြီးနားလည်ထားရမယ့် အချက်

Utility & Mapped Types မှာ caller နဲ့ implementation ကြား contract ကို minimal ဖြစ်အောင်ရေးပါ။ Generic constraints, variance, readonly data နဲ့ structural compatibility ကိုစဉ်းစားပြီး type assertion ဖြင့်အမှန်တရားကိုဖုံးမထားပါနှင့်။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

Utility & Mapped Types example ကို strict mode ဖြင့် type-check လုပ်ပြီး valid, null/undefined, malformed API data, union variant အသစ်နဲ့ module boundary cases စမ်းပါ။ Type error ကို assertion သို့ any ဖြင့်ပိတ်မထားဘဲ contract သို့ validation ကိုပြင်ပါ။

ဒီသင်ခန်းစာပြီးရင်

typescript
type User = { id: string; name: string; email: string };
type UserPatch = Partial<Pick<User, 'name' | 'email'>>;
type Flags<T> = { readonly [K in keyof T]: boolean };

const changed: Flags<UserPatch> = { name: true, email: false };
You should see
Source contract တစ်ခုမှ safe derived types များကို ပြန်သုံးနိုင်မည်။

ကိုယ်တိုင်စမ်းကြည့်ရန်

Utility & Mapped Types example ကို strict mode ဖြင့် type-check လုပ်ပြီး valid, null/undefined, malformed API data, union variant အသစ်နဲ့ module boundary cases စမ်းပါ။ Type error ကို assertion သို့ any ဖြင့်ပိတ်မထားဘဲ contract သို့ validation ကိုပြင်ပါ။

Type Safety သတိပေးချက်

TypeScript compile အောင်ရုံဖြင့် API data နဲ့ runtime behavior မှန်ပြီဟုယူဆခြင်း။

Utility TypesTypeScript

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • TypeScript compile အောင်ရုံဖြင့် API data နဲ့ runtime behavior မှန်ပြီဟုယူဆခြင်း။
  • Error ကို any, as သို့ non-null assertion ဖြင့်ဖျောက်ပြီး contract မပြင်ခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Utility & Mapped Types example ကို strict mode ဖြင့် type-check လုပ်ပြီး valid, null/undefined, malformed API data, union variant အသစ်နဲ့ module boundary cases စမ်းပါ။ Type error ကို assertion သို့ any ဖြင့်ပိတ်မထားဘဲ contract သို့ validation ကိုပြင်ပါ။

You'll know it worked when: Source contract တစ်ခုမှ safe derived types များကို ပြန်သုံးနိုင်မည်။

Utility & Mapped Types | Thuta Learning