Thuta Learning
TypeScript
AdvancedProgrammingအလယ်အလတ်

Utility & Mapped Types

TypeScriptသင်ခန်းစာ 2030 မိနစ်

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

  • keyof, indexed access T[K] နဲ့ [K in keyof T] ကို သုံးပြီး mapped type တစ်ခု ရေးနိုင်ဖို့
  • Partial, Required, Readonly, Pick, Omit, Record တို့ကို ပေါင်းစပ်ပြီး update နဲ့ form type တွေ ဆောက်နိုင်ဖို့
  • + / - modifier နဲ့ as key remapping ကို သုံးပြီး optional, readonly နဲ့ key နာမည်တွေကို ပြောင်းနိုင်ဖို့
  • Readonly က shallow ဖြစ်တာ၊ Omit က typo မဖမ်းတာလို ကန့်သတ်ချက်တွေကို ရှောင်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Utility နဲ့ mapped type ကို ရိုးရိုးလေး ပြောရရင်

Spreadsheet တစ်ခုမှာ formula column တစ်ခုကို စဉ်းစားကြည့်ပါ။ တန်ဖိုးတွေကို ကိုယ်တိုင် ရိုက်ထည့်မယ့်အစား 'ဘယ်ဘက် column ကို ယူပြီး တစ်ခုခု လုပ်' လို့ ပြောလိုက်တာနဲ့ မူရင်း column ပြောင်းတိုင်း အလိုအလျောက် လိုက်ပြောင်းပါတယ်။ Mapped type က type တွေအတွက် အဲဒီ formula ပါပဲ — 'User ရဲ့ property တိုင်းကို ယူပြီး optional ဖြစ်အောင် လုပ်' လို့ ပြောလိုက်ရင် User ပြောင်းတိုင်း ရလဒ် type လည်း လိုက်ပြောင်းပါတယ်။

Utility type တွေကတော့ TypeScript က နာမည်ပေးပြီး ကြိုထည့်ထားတဲ့ အသုံးများ formula တွေပါ: Partial, Required, Readonly, Pick, Omit နဲ့ Record တို့ဖြစ်ပါတယ်။ ကိုက်ညီတဲ့ formula မရှိတဲ့အခါမှ ကိုယ်ပိုင် mapped type ကို ရေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: လက်နဲ့ ကူးရေးထားတဲ့ type တွေ ကွဲထွက်သွားခြင်း

သင့် User type ထဲကို avatarUrl field အသစ် ထည့်လိုက်ပါတယ်။ Update form type, validation-error type နဲ့ table column flag type တွေကိုတော့ သီးခြား type တွေအဖြစ် လက်နဲ့ ရေးထားခဲ့တဲ့အတွက် တစ်ခုမှ field အသစ်အကြောင်း မသိကြပါဘူး။ Form က အဲဒီ field ကို တိတ်တဆိတ် လျစ်လျူရှုပြီး ဘာမှလည်း compile error မဖြစ်ပါဘူး — copy တစ်ခုချင်းစီက ကိုယ့်ဘာသာ မှန်ကန်နေပြီး User နဲ့သာ မကိုက်ညီတော့တာပါ။

user-types.ts
type User = { id: string; name: string; email: string; avatarUrl: string };

// Hand-written copies, each one a chance to drift:
type UserUpdate = { name?: string; email?: string }; // forgot avatarUrl
type UserErrors = { name?: string; email?: string }; // forgot it here too
type UserFlags = { id: boolean; name: boolean; email: boolean }; // and here

ဘာကြောင့် ဒါက ဆိုးတာလဲ

လက်နဲ့ရေးထားတဲ့ copy တိုင်းက source of truth နောက်တစ်ခု ဖြစ်သွားပါတယ်။ TypeScript က value တစ်ခုချင်းစီ ကိုယ့် type နဲ့ ကိုက်မကိုက် စစ်ပေးပေမဲ့ UserUpdate က User ကို ထင်ဟပ်ရမယ်ဆိုတာ မသိနိုင်ပါဘူး။ Type တွေကို derive လုပ်လိုက်ရင် (Partial<Omit<User, "id">>၊ keyof User ပေါ်က mapped type) ဆက်သွယ်ချက်က ထင်ရှားသွားပြီး User ပြောင်းတဲ့အခါ မှီခိုနေတဲ့ type တိုင်းကို compiler က update လုပ်ပေး ဒါမှမဟုတ် error ပြပေးပါတယ်။

Core concept: key တွေကို ပတ်ပြီး type အသစ် ဆောက်ခြင်း

Mapped type က key အစုတစ်ခုကို တစ်ခုချင်း ပတ်ပြီး key တစ်ခုစီအတွက် property အသစ် ဆောက်ပါတယ်။ { [K in keyof T]: T[K] } ကို 'T ရဲ့ key K တိုင်းအတွက် type က T[K] ဖြစ်တဲ့ property K ကို ဆောက်' လို့ ဖတ်ပါ။ keyof T က T ရဲ့ property နာမည်တွေကို union အဖြစ် ထုတ်ပေးပြီး T[K] (indexed access type) က property တစ်ခုရဲ့ type ကို ရှာပေးပါတယ်။ ညာဘက်ခြမ်းကို ပြောင်းလိုက်ရင် property အားလုံး တစ်ပြိုင်နက် ပြောင်းပါတယ်: { [K in keyof T]: boolean } က field တိုင်းကို flag အဖြစ် ပြောင်းပေးပါတယ်။ ဒါက compile time မှာပဲ ဖြစ်တာပါ — mapped type က JavaScript code ဘာမှ မထုတ်ပေးပါဘူး၊ ရလဒ် object ကိုတော့ သင့် function တွေက ဆက်ပြီး ဆောက်ရပါတယ်။

Built-in utility type တွေက နာမည်ပေးထားတဲ့ mapped type တွေပဲ ဖြစ်ပါတယ်။ Partial<T> က property တိုင်းကို ? ထည့်ပေးပြီး Required<T> က ဖယ်ပေး၊ Readonly<T> က readonly ထည့်ပေးပါတယ်။ Pick<T, K> က K ထဲက key တွေကိုပဲ ချန်ထားပြီး Omit<T, K> က ကျန်တာတွေကို ချန်ထား၊ Record<K, V> ကတော့ key K နဲ့ value V ပါတဲ့ object type ကို ဆောက်ပေးပါတယ်။ ပေါင်းစပ်လို့ ရတဲ့အတွက် Partial<Pick<User, "name" | "email">> ဆိုတာ 'name နဲ့/ဒါမှမဟုတ် email ပါနိုင်ပြီး တခြားဘာမှ မပါတဲ့ object' လို့ အဓိပ္ပာယ်ရပါတယ်။

UtilityUser အပေါ် ရလဒ်
Partial<User>property တိုင်း optional
Readonly<User>property တိုင်း readonly (အဆင့်တစ်ဆင့်သာ)
Pick<User, "id" | "name">id နဲ့ name ပဲ ကျန်
Omit<User, "email">email ကလွဲပြီး အားလုံး
Record<"admin" | "guest", User>admin နဲ့ guest key ပါပြီး တစ်ခုစီက User

keyof T ပေါ်မှာ ဆောက်ထားတဲ့ mapped type တွေကို homomorphic လို့ ခေါ်ပြီး မူရင်း modifier တွေကို ကူးယူပါတယ်: T ထဲမှာ property တစ်ခုက optional ဒါမှမဟုတ် readonly ဖြစ်ခဲ့ရင် သင်မပြောင်းမချင်း အဲဒီအတိုင်း ဆက်ရှိနေပါတယ်။ Modifier တွေကို + နဲ့ - သုံးပြီး တိုက်ရိုက် ထည့်/ဖယ် လုပ်လို့ ရပါတယ်: { -readonly [K in keyof T]: T[K] } က အားလုံးကို ပြင်လို့ရအောင် လုပ်ပေးပြီး { [K in keyof T]-?: T[K] } က အားလုံးကို required ဖြစ်စေပါတယ် — Required<T> ကို အဲဒီအတိုင်း define လုပ်ထားတာပါ။

Mapped type ကို type တွေအတွက် formula လို မြင်ကြည့်ပါ
  1. Source type တစ်ခုကနေ စ
  2. keyof က property နာမည်တွေ ထုတ်ပေး
  3. [K in ...] က key တစ်ခုချင်း လှည့်
  4. Key တစ်ခုစီအတွက် type အသစ် ရေး

Utility type တွေက နာမည်ပေးထားတဲ့ mapped type တွေပါ၊ built-in တွေ မကိုက်ရင် mapping ကို ကိုယ်တိုင် ရေးပါ။

အထွေထွေ ပုံစံ

typescript
type Mapped<T> = { [K in keyof T]: NewType };

type Optional<T> = { [K in keyof T]?: T[K] };
type Writable<T> = { -readonly [K in keyof T]: T[K] };
type Renamed<T> = { [K in keyof T as NewKeyName]: T[K] };

type Derived = Partial<Pick<Source, "keyA" | "keyB">>;

[K in keyof T] က key တိုင်းကို လှည့်ပြီး ညာဘက်က type က property အသစ်ရဲ့ type ဖြစ်ပါတယ်။ ? နဲ့ readonly ကို +/- နဲ့ ထိန်းချုပ်ပြီး as နဲ့ key နာမည်ကို ပြောင်းလို့ ရပါတယ်။ Built-in တွေကိုတော့ တစ်ခုထဲ တစ်ခု ထည့်ပြီး ပေါင်းသုံးပါ။

Worked example: User တစ်ခုကနေ patch နဲ့ change flag များ

ဒီ example မှာ User တစ်ခုတည်းက source ဖြစ်ပါတယ်။ UserPatch က ပြင်လို့ရတဲ့ field တွေကို optional အဖြစ် derive လုပ်ပြီး Flags<T> ကိုယ်ပိုင် mapped type က ဘယ် field ပြောင်းသွားလဲ မှတ်ပါတယ်။ အောက်ဆုံးက comment ထားတဲ့ line နှစ်ကြောင်းက derived type တွေ ကာကွယ်ပေးတဲ့ အမှားတွေကို ပြပါတယ်။

utility-mapped.ts
type User = { id: string; name: string; email: string };
type UserPatch = Partial<Pick<User, "name" | "email">>;
type Flags<T> = { readonly [K in keyof T]: boolean };

function applyPatch(user: User, patch: UserPatch): User {
  return { ...user, ...patch };
}

function changedFields(before: User, after: User): Flags<UserPatch> {
  return {
    name: before.name !== after.name,
    email: before.email !== after.email,
  };
}

const ada: User = { id: "u1", name: "Ada", email: "ada@old.dev" };
const updated = applyPatch(ada, { email: "ada@new.dev" });
const changed = changedFields(ada, updated);

console.log(updated);
console.log(changed);
// changed.name = false; // error: name is readonly
// applyPatch(ada, { id: "u2" }); // error: id is not part of UserPatch

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Compose utilitiestype UserPatch = Partial<Pick<User, "name" | "email">>;

    Pick က name နဲ့ email ကိုပဲ ချန်ပြီး Partial က နှစ်ခုလုံးကို optional ဖြစ်စေပါတယ်။ id ကို patch ထဲ ထည့်ခွင့် မရှိပါဘူး။

  2. Custom mapped typetype Flags<T> = { readonly [K in keyof T]: boolean };

    T ရဲ့ key တိုင်းအတွက် readonly boolean တစ်ခု ဆောက်ပါတယ်။ keyof ပေါ်မှာ ဆောက်ထားလို့ UserPatch ရဲ့ ? modifier ကိုလည်း ကူးယူပါတယ်။

  3. Merge a patchreturn { ...user, ...patch };

    Patch ထဲမှာ ပါတဲ့ field တွေကပဲ user ရဲ့ field တွေကို အစားထိုးပါတယ်။ { email } တစ်ခုတည်း ပို့ရင် name မပြောင်းပါဘူး။

  4. Return the flagsfunction changedFields(before: User, after: User): Flags<UserPatch> {

    Return type က UserPatch ကနေ derive လုပ်ထားလို့ UserPatch ထဲ field အသစ် ထည့်တဲ့နေ့မှာ ဒီ object က အလိုအလျောက် အဲဒီ field အတွက် flag ကို လက်ခံပါလိမ့်မယ်။

You should see
{ id: 'u1', name: 'Ada', email: 'ada@new.dev' }
{ name: false, email: true }

Modifier နဲ့ key remapping ပါတဲ့ ကိုယ်ပိုင် mapped type

Built-in တွေ မလုံလောက်တဲ့အခါ mapping ကို ကိုယ်တိုင် ရေးပါ။ Mutable<T> က -readonly နဲ့ readonly ကို ဖယ်ပေးပြီး Getters<T> ကတော့ as clause နဲ့ template literal type ကို သုံးပြီး key နာမည်တွေကို ပြောင်းပါတယ် — theme က getTheme ဖြစ်သွားပြီး value ကတော့ T[K] ကို ပြန်ပေးတဲ့ function ဖြစ်ပါတယ်။

custom-mapped.ts
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};

type Settings = { readonly theme: string; readonly fontSize: number };

function makeGetters(settings: Settings): Getters<Settings> {
  return {
    getTheme: () => settings.theme,
    getFontSize: () => settings.fontSize,
  };
}

const draft: Mutable<Settings> = { theme: "dark", fontSize: 14 };
draft.fontSize = 16; // allowed: readonly was removed
const getters = makeGetters(draft);
console.log(getters.getTheme(), getters.getFontSize());

string & K ဆိုတဲ့ အပိုင်းက လိုအပ်ပါတယ်၊ keyof က number နဲ့ symbol key တွေကိုလည်း ထုတ်ပေးနိုင်ပြီး Capitalize က string ကိုပဲ လက်ခံလို့ပါ။ as clause က never ကို ပြန်ပေးရင် အဲဒီ key ကို ဖယ်ထုတ်လိုက်ပါတယ်၊ ဒါကြောင့် key တွေကို filter လုပ်ဖို့လည်း သုံးလို့ ရပါတယ်။

လက်တွေ့ project တွေမှာ

လက်တွေ့ codebase တွေမှာ derived type များ

Production TypeScript မှာ derived type တွေကို နေရာတိုင်းမှာ တွေ့ရပါတယ်။ Codebase တော်တော်များများမှာ တွေ့ရမယ့် pattern အချို့ကတော့ ဒီလိုပါ:

  • PATCH endpoint နဲ့ update form: Partial<Omit<User, "id" | "createdAt">> က ပြင်လို့ရတဲ့ field တွေရဲ့ ဘယ် subset ကိုမဆို လက်ခံပြီး id ကို ပြောင်းဖို့ ကြိုးစားတာကို ငြင်းပါတယ်။
  • Form state: { [K in keyof Form]?: string } လို mapped type က field တစ်ခုစီအတွက် optional error message တစ်ခုစီ သိမ်းပြီး { [K in keyof Form]: boolean } က ဘယ် field တွေကို user ထိပြီးပြီလဲ မှတ်ပါတယ်။
  • Lookup table: label ဒါမှမဟုတ် colour အတွက် Record<Status, string> က status တိုင်းအတွက် entry ပေးဖို့ အတင်းတောင်းပါတယ်၊ ဒါကြောင့် status အသစ်ထည့်ရင် table ကို update မလုပ်မချင်း build ကျိုးပါတယ်။
  • Library API: ORM နဲ့ state library တွေက model definition တစ်ခုတည်းကနေ typed selector, getter နဲ့ query result တွေ ထုတ်ပေးဖို့ Readonly, Pick နဲ့ key remapping ကို သုံးပါတယ်။

အမှားများ

Utility နဲ့ mapped type တွေမှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

Readonly က အတွင်းထိ ရောက်တယ်လို့ ထင်ခြင်း

Readonly<Order> က order.items = [] ကို တားပေမဲ့ order.items.push(x) ကို မတားပါဘူး၊ အပေါ်ဆုံးအဆင့် property တွေပဲ readonly ဖြစ်လို့ပါ။ Nested data အတွက် type ထဲမှာ readonly string[] လို readonly array ကို သုံးပါ၊ ဒါမှမဟုတ် recursive DeepReadonly type တစ်ခု ရေးပါ။

Omit က typo ဖမ်းပေးမယ်လို့ ယုံခြင်း

Omit<User, "emial"> က compile ဖြစ်ပြီး ဘာမှ မဖယ်ပါဘူး၊ Omit က ဘယ် string key ကိုမဆို လက်ခံလို့ပါ။ Pick ကတော့ ရှိတဲ့ key တွေကိုပဲ တောင်းပါတယ် (K extends keyof T)။ Typo-safe omit လိုရင် type StrictOmit<T, K extends keyof T> = Omit<T, K> လို့ ရေးပါ။

Partial က undefined ကို တားတယ်လို့ ထင်ခြင်း

exactOptionalPropertyTypes compiler option မဖွင့်ထားရင် { name: undefined } က valid Partial<User> ဖြစ်ပြီး user ပေါ်ကို spread လုပ်လိုက်တဲ့အခါ name ကို undefined နဲ့ overwrite လုပ်သွားပါတယ်။ Merge မလုပ်ခင် undefined value တွေကို ဖယ်ပါ၊ ဒါမှမဟုတ် အဲဒီ option ကို ဖွင့်ပါ။

Best Practices

Best practices

  • Domain model တစ်ခုစီအတွက် source type တစ်ခုတည်း ထားပြီး form, patch, view type တွေကို အဲဒီကနေ derive လုပ်ပါ။
  • Derived type တွေကို UserPatch, UserFormErrors လို နာမည်ပေးထားပါ၊ ဒါမှ error message တွေ ဖတ်ရလွယ်ပြီး ဘာအတွက်လဲ ထင်ရှားပါတယ်။
  • Built-in utility type တစ်ခုက ကိုက်ညီရင် ကိုယ်ပိုင် mapped type အစား အဲဒါကို သုံးပါ၊ team တစ်ခုလုံး ချက်ချင်း နားလည်ပါတယ်။
  • Utility type တွေကို အဆင့် သုံးဆင့်ထက် ပိုမထပ်ပါနဲ့၊ ရှုပ်လာရင် အလယ်က အဆင့်ကို နာမည်ပေးထားတဲ့ alias အဖြစ် ခွဲထုတ်ပါ။

Mini challenge

Mini challenge: form တိုင်းအတွက် FormErrors<T>

Sign-up form တစ်ခုမှာ username, email နဲ့ age field တွေ ရှိပါတယ်။ ဘယ် form type အတွက်မဆို field တစ်ခုစီရဲ့ optional error message ကို သိမ်းနိုင်တဲ့ FormErrors<T> mapped type ကို ဆောက်ပါ၊ ပြီးရင် ရှိတဲ့ field တွေအတွက်ပဲ error report လုပ်နိုင်တဲ့ validateSignUp ကို ရေးပါ။

  • FormErrors<T> ကို keyof T ပေါ်က mapped type နဲ့ define လုပ်ပါ (value တစ်ခုစီက optional string)
  • validateSignUp(form: SignUp) က FormErrors<SignUp> ကို ပြန်ပေးရမယ်
  • စာလုံး ၃ လုံးအောက် username၊ @ မပါတဲ့ email နဲ့ ၁၃ နှစ်အောက် age ကို ငြင်းပါ
  • မမှန်တဲ့ form တစ်ခုနဲ့ မှန်တဲ့ form တစ်ခုအတွက် error တွေကို print ထုတ်ပါ
form-errors.ts
type SignUp = { username: string; email: string; age: number };

// TODO: one optional string message per key of T
type FormErrors<T> = unknown;

function validateSignUp(form: SignUp): FormErrors<SignUp> {
  // TODO: collect a message for each invalid field
  return {};
}

// TODO: print the result for an invalid form and a valid form

ရလဒ်: မမှန်တဲ့ form က username, email, age သုံးခုလုံးအတွက် message ပါတဲ့ object ကို print ထုတ်ပြီး မှန်တဲ့ form က {} ကို print ထုတ်ပါတယ်။ errors.password = "x" လို့ ရေးကြည့်ရင် compile error ဖြစ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

Partial<T> ကို ဘယ်လို ရေးထားလဲ ပြန်စဉ်းစားပါ: { [K in keyof T]?: T[K] }။ ဒီမှာ value type ကိုပဲ string လို့ ပြောင်းဖို့ လိုပါတယ်။ Errors object ကို {} နဲ့ စပြီး field တစ်ခုချင်း စစ်ပါ။

အဖြေ ကြည့်ရန်
form-errors.ts
type SignUp = { username: string; email: string; age: number };

type FormErrors<T> = { [K in keyof T]?: string };

function validateSignUp(form: SignUp): FormErrors<SignUp> {
  const errors: FormErrors<SignUp> = {};
  if (form.username.trim().length < 3) errors.username = "At least 3 characters";
  if (!form.email.includes("@")) errors.email = "Must contain @";
  if (form.age < 13) errors.age = "Must be 13 or older";
  // errors.password = "x"; // error: password is not a key of SignUp
  return errors;
}

console.log(validateSignUp({ username: "al", email: "al.example.com", age: 12 }));
console.log(validateSignUp({ username: "alice", email: "alice@example.com", age: 30 }));

Quick Quiz 1 / 3

type Flags<T> = { readonly [K in keyof T]: boolean } ဆိုရင် Flags<{ name?: string; age: number }> က ဘာဖြစ်မလဲ?

Quick Quiz 2 / 3

type Order = Readonly<{ id: string; items: string[] }> ဖြစ်ပါတယ်။ order: Order အတွက် ဘယ် line က compile ဖြစ်လဲ?

Quick Quiz 3 / 3

PATCH /users/:id handler က name နဲ့ email ထဲက ကြိုက်တာကို ပြင်ခွင့်ရှိပြီး id ကိုတော့ ဘယ်တော့မှ မပြင်ရပါဘူး။ Request body အတွက် ဘယ် type က ကိုက်လဲ?

မှတ်ထားရမယ့် အချက်များ

  • Mapped type { [K in keyof T]: ... } က T ရဲ့ key တိုင်းကို လှည့်ပြီး property တစ်ခုစီရဲ့ type အသစ်ကို ဆောက်ပါတယ်။
  • Partial, Required, Readonly, Pick, Omit, Record တို့က နာမည်ပေးထားတဲ့ mapped type တွေဖြစ်ပြီး ပေါင်းစပ်သုံးလို့ ရပါတယ်။
  • Homomorphic mapped type တွေက ? နဲ့ readonly ကို ကူးယူပါတယ်၊ +/- နဲ့ ပြောင်းပြီး as နဲ့ key နာမည် ပြောင်းပါ။
  • Readonly က shallow ဖြစ်ပြီး Omit က key typo ကို မဖမ်းပါဘူး၊ Partial ကလည်း explicit undefined ကို လက်ခံပါတယ်။

Utility Types — TypeScript

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Utility & Mapped Types | Thuta Learning