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

Objects

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

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

  • Inline object type တစ်ခု ရေးပြီး type alias နဲ့ နာမည်ပေးကာ ပြန်သုံးနိုင်ဖို့
  • Optional (?) နဲ့ readonly property တွေကို သုံးပြီး optional value ကို undefined check နဲ့ ကိုင်တွယ်နိုင်ဖို့
  • Excess property check နဲ့ structural typing ဘယ်လို အလုပ်လုပ်လဲ ရှင်းပြနိုင်ဖို့
  • Key တွေကို ကြိုမသိတဲ့ dictionary အတွက် Record<string, T> ကို သုံးနိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

Object type ကို ဖြည့်ရမယ့် form တစ်ခုရဲ့ ပုံစံ (template) လို့ မြင်ကြည့်ပါ။ Form မှာ "နာမည်: စာသား"၊ "အသက်: ဂဏန်း" လို့ အကွက်တွေ ကြိုသတ်မှတ်ထားသလို { name: string; age: number } ကလည်း object မှာ name နဲ့ age ဆိုတဲ့ property နှစ်ခု ရှိရမယ်၊ ဘယ်ဟာက ဘာ type ဖြစ်ရမယ်လို့ ပြောပါတယ်။

Form ကို ဖြည့်တဲ့အခါ အကွက်တစ်ခု ကျန်ခဲ့ရင်၊ စာလုံးပေါင်း မှားရင် ဒါမှမဟုတ် ဂဏန်းနေရာမှာ စာသား ထည့်ရင် TypeScript က ချက်ချင်း ထောက်ပြပါတယ်။ Property ကို ဖတ်တဲ့အခါမှာလည်း မရှိတဲ့ property ကို ဖတ်မိရင် သတိပေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: စာလုံးပေါင်းမှားတာက undefined ဖြစ်သွားတယ်

JavaScript မှာ object ပေါ်က မရှိတဲ့ property ကို ဖတ်ရင် error မတက်ဘဲ undefined ပဲ ပြန်လာပါတယ်။ ဒါကြောင့် email ကို emial လို့ စာလုံးပေါင်းမှားတာ တစ်ခုက welcome email ကို "undefined" ဆိုတဲ့ လိပ်စာဆီ ပို့ဖို့ ကြိုးစားစေပါတယ်။ Object ဆောက်တဲ့ဘက်မှာလည်း field တစ်ခု ကျန်ခဲ့တာ ဒါမှမဟုတ် အသက်ကို string အဖြစ် ထည့်မိတာကို ဘယ်သူမှ မသိပါဘူး၊ နောက်မှ ဒီ data ကို သုံးတဲ့ code ကျမှ ထူးဆန်းတဲ့ ရလဒ်တွေ ထွက်လာပါတယ်။

signup.js
// Plain JavaScript: the object's shape lives only in your head
const user = { name: "Aye Aye", email: "aye@example.com", age: "29" };

function sendWelcome(u) {
  console.log("Sending to " + u.emial); // typo: prints "Sending to undefined"
}

sendWelcome(user);
const nextYear = user.age + 1; // "291", not 30

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

JavaScript object တွေမှာ ကြိုသတ်မှတ်ထားတဲ့ shape မရှိလို့ပါ — ဘယ် property ကိုမဆို ဖတ်လို့ရပြီး ဘယ် type ကိုမဆို ထည့်လို့ရပါတယ်။ Object type တစ်ခု ပေးလိုက်ရင် TypeScript က u.emial မှာ "Property 'emial' does not exist... Did you mean 'email'?" လို့ ပြောပြီး age: "29" ကိုလည်း number မဟုတ်လို့ ငြင်းပါတယ်။

Core concept: object type က shape တစ်ခုကို ဖော်ပြတယ်

Object type ကို { property: Type; property: Type } ပုံစံနဲ့ ရေးပါတယ်။ Variable ပေါ်မှာ တိုက်ရိုက် (inline) ရေးလို့ရပေမယ့် နေရာများစွာမှာ သုံးမယ်ဆိုရင် type User = { ... } လို type alias နဲ့ နာမည်ပေးထားတာ ပိုကောင်းပါတယ်။ ဒါဆို function parameter, return type နဲ့ array element တွေမှာ User လို့ပဲ ရေးလိုက်ရုံပါ။ (interface ဆိုတဲ့ ဆင်တူ syntax တစ်ခုလည်း ရှိပြီး နောက် lesson မှာ လေ့လာပါမယ်။)

Property တွေကို ပိုတိကျအောင် modifier နှစ်ခု ထည့်လို့ရပါတယ်။ nickname?: string ဆိုတဲ့ ? က property မပါလည်း ရတယ်လို့ ဆိုလိုပြီး ဖတ်တဲ့အခါ type က string | undefined ဖြစ်ပါတယ်၊ ဒါကြောင့် သုံးခင် ?? ဒါမှမဟုတ် if နဲ့ စစ်ရပါမယ်။ readonly id: number ကတော့ object ဆောက်ပြီးနောက် ပြန်ပြောင်းခွင့် မပေးပါဘူး။ ဒါပေမယ့် readonly က compile time မှာပဲ စစ်တာဖြစ်ပြီး shallow ပါ — readonly tags: string[] ဆိုရင် tags ကို အသစ် assign လုပ်လို့ မရပေမယ့် tags.push() ကတော့ ရနေပါသေးတယ်။

TypeScript က object တွေကို structural typing နဲ့ စစ်ပါတယ် — နာမည်ကို မကြည့်ဘဲ shape ကိုပဲ ကြည့်ပါတယ်။ User လိုအပ်တဲ့ နေရာမှာ User ရဲ့ property အားလုံး ပါတဲ့ object ဘယ်ဟာမဆို လက်ခံပြီး property အပို ပါနေလည်း ရပါတယ်။ ခြွင်းချက်တစ်ခု ရှိပါတယ် — object literal ကို type ရှိတဲ့ နေရာဆီ တိုက်ရိုက် ရေးလိုက်ရင် excess property check အလုပ်လုပ်ပြီး { name: "A", age: 3 } ကို { name: string } ထဲ ထည့်ရင် "Object literal may only specify known properties" error ပေးပါတယ်။ တိုက်ရိုက်ရေးတဲ့ literal မှာ property အပိုဆိုတာ စာလုံးပေါင်းမှားတာ ဖြစ်နိုင်ခြေ များလို့ပါ။

စစ်ဆေးမှုက ဘက်နှစ်ဖက်လုံးမှာ ဖြစ်ပါတယ်။ Object ဆောက်တဲ့အခါ required property တစ်ခု ကျန်ရင် "Property 'age' is missing" ရပြီး type မှားရင် "Type 'string' is not assignable to type 'number'" ရပါတယ်။ ဖတ်တဲ့အခါ type မှာ မရှိတဲ့ property ကို ဖတ်ရင် "Property 'emial' does not exist" ရပါတယ်။ Nested object တွေမှာလည်း အဆင့်တိုင်းကို ဒီအတိုင်း စစ်ပါတယ် — user.address.city ရဲ့ city ကိုပါ စာလုံးပေါင်း စစ်ပါတယ်။

မှတ်ထားရမယ့် ပုံစံ
  1. Type က form ရဲ့ အကွက်တွေကို သတ်မှတ်
  2. Object ဆောက်ရင် အကွက်တိုင်း စစ်
  3. ? အကွက်က undefined ဖြစ်နိုင်
  4. Property ဖတ်ရင် နာမည်ကို စစ်

Type ကို ကြေညာထားတဲ့ shape နဲ့ မကိုက်ရင် ဆောက်တဲ့နေရာမှာရော ဖတ်တဲ့နေရာမှာပါ compile error ရပါတယ်။

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

typescript
type Name = {
  required: Type;
  optional?: Type;          // Type | undefined when read
  readonly fixed: Type;     // cannot be reassigned
  nested: { inner: Type };
};

const value: Name = { required: ..., fixed: ..., nested: { inner: ... } };

Property တစ်ခုချင်းကို နာမည်: Type; နဲ့ ရေးပါတယ်။ ? က optional ဖြစ်စေပြီး readonly က ပြန်မပြောင်းနိုင်အောင် လုပ်ပါတယ်။ Object type တွေကို အထပ်ထပ် nest လုပ်လို့ ရပါတယ်။

Example: inline object type

အရိုးရှင်းဆုံး ပုံစံက type ကို variable ပေါ်မှာ တိုက်ရိုက် ရေးတာပါ။ ဒီ object ကို တစ်နေရာတည်းမှာပဲ သုံးလို့ alias မလိုသေးပါဘူး။ နောက်ပိုင်း deeper section မှာ ဒီ shape ကို နာမည်ပေးပြီး ပြန်သုံးပုံကို ကြည့်ပါမယ်။

typescript
const person: { name: string; age: number } = {
  name: "John Doe",
  age: 30
};

console.log(`${person.name} is ${person.age} years old.`);

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

  1. Line 1const person: { name: string; age: number } = {

    Colon နောက်က { name: string; age: number } က inline object type ပါ။ person မှာ property နှစ်ခုလုံး ရှိရပြီး type မှန်ရပါမယ်။

  2. Line 2name: "John Doe",

    name က string ဖြစ်ရပါမယ်။ nmae လို စာလုံးပေါင်းမှားရင် "'nmae' does not exist in type" ဆိုတဲ့ excess property error က အဲဒီ line ကို တိုက်ရိုက် ညွှန်ပြပါတယ်။

  3. Line 3age: 30

    age က number ပါ။ age: "30" လို string ထည့်ရင် "Type 'string' is not assignable to type 'number'" error တက်ပါတယ်။

  4. Line 6console.log(`${person.name} is ${person.age} years old.`);

    person.name နဲ့ person.age ကို ဖတ်တဲ့အခါ editor က autocomplete ပေးပြီး person.email လို မရှိတဲ့ property ကို ဖတ်ရင် error ပြပါတယ်။

You should see
John Doe is 30 years old.

Type alias, optional, readonly နဲ့ nested object

User type ကို တစ်ခါ ကြေညာပြီး object နှစ်ခုနဲ့ function တစ်ခုမှာ ပြန်သုံးထားပါတယ်။ nickname က optional ဖြစ်လို့ displayName() က ?? နဲ့ name ကို fallback လုပ်ပါတယ်။ Spread (...aye) နဲ့ object အသစ် ဆောက်တဲ့အခါမှာလည်း ရလဒ်ကို User shape နဲ့ စစ်ပါတယ်။

user.ts
type User = {
  readonly id: number;
  name: string;
  nickname?: string;
  address: { city: string; country: string };
};

function displayName(user: User): string {
  return user.nickname ?? user.name;
}

const aye: User = {
  id: 1,
  name: "Aye Aye",
  address: { city: "Yangon", country: "Myanmar" },
};
const ko: User = { ...aye, id: 2, name: "Ko Ko", nickname: "KK" };

console.log(displayName(aye), "-", aye.address.city);
console.log(displayName(ko), "-", ko.address.country);

Key တွေကို ကြိုမသိရင်: Record

ကုန်ပစ္စည်း stock စာရင်းလို key တွေက runtime မှာမှ ပေါ်လာတဲ့ object တွေအတွက် Record<string, number> ကို သုံးပါတယ်။ ဒါက "string key ဘယ်နှစ်ခုမဆို၊ value တိုင်းက number" လို့ ဆိုလိုပါတယ်။ သတိထားရမှာက မရှိတဲ့ key ကို ဖတ်ရင်လည်း type က number လို့ ပြနေပေမယ့် value က undefined ဖြစ်နေတာပါ။

stock.ts
const stock: Record<string, number> = { apple: 12, mango: 0 };
stock.banana = 30;

for (const [fruit, count] of Object.entries(stock)) {
  console.log(`${fruit}: ${count}`);
}

const papaya = stock["papaya"]; // typed number, value undefined
console.log(papaya);

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

Real project တွေမှာ ဘယ်လို သုံးလဲ

Real app တိုင်းမှာ domain object တွေ ရှိပါတယ် — User, Product, Order, Invoice စတာတွေပါ။ ဒီ shape တွေကို types.ts လို file တစ်ခုမှာ type alias အဖြစ် တစ်နေရာတည်း ကြေညာပြီး React component ရဲ့ props၊ API response၊ database row နဲ့ form state တွေမှာ ပြန်သုံးပါတယ်။ Order မှာ field အသစ် ထည့်တာ ဒါမှမဟုတ် နာမည်ပြောင်းတာနဲ့ အဲဒီ shape ကို သုံးထားတဲ့ နေရာတိုင်းမှာ compiler က ပြင်ရမယ့်နေရာကို ပြပေးပါတယ်။

Optional property တွေက "အမြဲ မရှိတဲ့" data ကို ရိုးရိုးသားသား ဖော်ပြပေးပါတယ် — ဥပမာ user က profile picture မတင်ရသေးတာ၊ order က ship မလုပ်ရသေးလို့ trackingNumber မရှိတာမျိုးပါ။ readonly ကိုတော့ id, createdAt လို ဘယ်တော့မှ မပြောင်းသင့်တဲ့ field တွေမှာ သုံးပါတယ်။ ဒါပေမယ့် server ကနေ ဝင်လာတဲ့ JSON က ဒီ type နဲ့ တကယ် ကိုက်မကိုက်ကို type annotation က runtime မှာ မစစ်ပေးလို့ API boundary မှာ validation သီးသန့် လုပ်ရပါမယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Optional property ကို မစစ်ဘဲ သုံးတာ

nickname?: string ဆိုရင် user.nickname.toUpperCase() က "'user.nickname' is possibly 'undefined'" error ပေးပါတယ်။ ! (non-null assertion) နဲ့ ပိတ်လိုက်ရင် nickname မရှိတဲ့ user တွေအတွက် runtime မှာ crash ဖြစ်ပါတယ်။ user.nickname?.toUpperCase() ဒါမှမဟုတ် ?? နဲ့ fallback ပေးပါ။

2. readonly က object ကို runtime မှာ freeze လုပ်တယ်လို့ ထင်တာ

readonly က compile time စစ်ဆေးမှုသက်သက်ပါ၊ JavaScript ထွက်လာရင် ပျောက်သွားပါတယ်။ Shallow လည်း ဖြစ်လို့ readonly items: string[] ဆိုရင် items.push() ရနေပါသေးတယ်။ Array ကိုပါ ကာကွယ်ချင်ရင် readonly string[] သုံးပြီး runtime မှာ တကယ် ပိတ်ချင်ရင် Object.freeze() သုံးပါ။

3. Excess property check က အမြဲ အလုပ်လုပ်တယ်လို့ ထင်တာ

Check က object literal ကို တိုက်ရိုက် ရေးတဲ့အခါမှာပဲ အလုပ်လုပ်ပါတယ်။ const extra = { name: "B", age: 4 }; ပြီးမှ { name: string } ထဲ ထည့်ရင် error မတက်ပါဘူး — structural typing အရ name ရှိရင် လုံလောက်လို့ပါ။ ဒါကြောင့် property အပိုတွေ (ဥပမာ password) ပါနေတဲ့ object ကို API ဆီ ပြန်ပို့မိနိုင်ပါတယ်၊ လိုတဲ့ field တွေကိုပဲ ရွေးကူးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Shape တစ်ခုကို နေရာ နှစ်ခုထက် ပိုသုံးရင် inline type အစား type alias နဲ့ နာမည်ပေးပါ။
  • "အမြဲ မရှိနိုင်တဲ့" field တွေကိုပဲ optional လုပ်ပါ၊ မလိုအပ်ဘဲ ? တွေ များနေရင် undefined check တွေ နေရာတိုင်းမှာ ပြန့်ကုန်ပါမယ်။
  • id, createdAt လို ဘယ်တော့မှ မပြောင်းသင့်တဲ့ field တွေကို readonly လုပ်ပါ။

Mini challenge

Mini challenge: ကုန်ပစ္စည်း catalog

Product type တစ်ခု ကြေညာပြီး discount ပါတဲ့ နောက်ဆုံးဈေးနှုန်းကို တွက်ကာ ကုန်ပစ္စည်းတစ်ခုချင်းကို စာကြောင်းတစ်ကြောင်းစီ print ထုတ်ပါ။

  • Product မှာ readonly sku, name, price နဲ့ optional discountPercent ပါရမယ်
  • finalPrice(product) က discountPercent မရှိရင် 0 လို့ ယူဆရမယ်
  • describe(product) က discount ရှိရင် " (-25%)" လို tag ထည့်ပေးရမယ်
  • ကုန်ပစ္စည်း နှစ်ခု (discount ပါတာ တစ်ခု၊ မပါတာ တစ်ခု) နဲ့ စမ်းပါ
catalog.ts
type Product = {
  // TODO: readonly sku, name, price, optional discountPercent
};

function finalPrice(product: Product): number {
  // TODO: apply discountPercent (missing means 0)
}

function describe(product: Product): string {
  // TODO: "MUG-02 Clay mug: 9.00 (-25%)"
}

// TODO: create two products and print describe() for each

ရလဒ်: TEA-01 Green tea: 4.00 MUG-02 Clay mug: 9.00 (-25%)

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

product.discountPercent ?? 0 က optional value ကို number ဖြစ်အောင် လုပ်ပေးပါတယ်။ ဈေးနှုန်းကို ဒသမ နှစ်နေရာနဲ့ ပြဖို့ toFixed(2) ကို သုံးပါ။

အဖြေ ကြည့်ရန်
catalog.ts
type Product = {
  readonly sku: string;
  name: string;
  price: number;
  discountPercent?: number;
};

function finalPrice(product: Product): number {
  const discount = product.discountPercent ?? 0;
  return product.price * (1 - discount / 100);
}

function describe(product: Product): string {
  const tag = product.discountPercent ? ` (-${product.discountPercent}%)` : "";
  return `${product.sku} ${product.name}: ${finalPrice(product).toFixed(2)}${tag}`;
}

const catalog: Product[] = [
  { sku: "TEA-01", name: "Green tea", price: 4 },
  { sku: "MUG-02", name: "Clay mug", price: 12, discountPercent: 25 },
];

for (const product of catalog) console.log(describe(product));

Quick Quiz 1 / 3

const p: { name: string } = { name: "Aye", age: 3 }; မှာ ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

type User = { nickname?: string } ဆိုရင် user.nickname ကို ဖတ်တဲ့အခါ type က ဘာလဲ။

Quick Quiz 3 / 3

const box: { readonly tags: string[] } = { tags: [] }; ဆိုရင် ဘယ် line က compile error ဖြစ်မလဲ။

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

  • Object type { prop: Type } က object မှာ ရှိရမယ့် property တွေနဲ့ type တွေကို ဖော်ပြပြီး type alias နဲ့ နာမည်ပေးကာ ပြန်သုံးနိုင်ပါတယ်။
  • ? က property ကို optional ဖြစ်စေပြီး (ဖတ်ရင် Type | undefined) readonly က ပြန် assign လုပ်ခွင့် မပေးပါ (compile time၊ shallow)။
  • TypeScript က shape ကို စစ်ပါတယ်၊ excess property check ကတော့ တိုက်ရိုက်ရေးတဲ့ object literal တွေမှာပဲ အလုပ်လုပ်ပါတယ်။
  • Key တွေကို ကြိုမသိရင် Record<string, T> ကို သုံးပြီး မရှိတဲ့ key တွေက undefined ဖြစ်နိုင်ကြောင်း သတိထားပါ။

TypeScript Handbook — TypeScript

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

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

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

Objects | Thuta Learning