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

Why Use TypeScript?

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

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

  • String နဲ့ number ရောနှောမိလို့ ဖြစ်တဲ့ bug တစ်ခုကို TypeScript က run မတိုင်ခင် ဘယ်လို ဖမ်းလဲ ပြနိုင်ဖို့
  • Static checking, autocomplete, maintainability ဆိုတဲ့ အားသာချက်သုံးခုကို ဥပမာတစ်ခုစီနဲ့ ရှင်းပြနိုင်ဖို့
  • Form ဒါမှမဟုတ် API ကလာတဲ့ data ကို boundary မှာ မှန်မှန်ကန်ကန် ပြောင်းနိုင်ဖို့ (`Number()` vs `as`)
  • TypeScript က မကာကွယ်ပေးနိုင်တဲ့ bug အမျိုးအစားတွေကို ခွဲခြားသိဖို့

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

ဘာကြောင့် TypeScript ကို သုံးကြလဲ

File ငါးခုလောက်ပဲ ရှိတဲ့ JavaScript project မှာ function တိုင်း ဘာလိုချင်လဲ ဆိုတာ ခေါင်းထဲမှာ မှတ်ထားလို့ ရပါတယ်။ File ငါးရာ၊ developer ဆယ်ယောက် ဖြစ်လာတဲ့အခါ အဲဒီ အချက်အလက်တွေက ခေါင်းထဲမှာ မဆံ့တော့ပါဘူး၊ code ရှုပ်လာပြီး bug တွေ ရှာရခက်လာပါတယ်။

TypeScript က အဲဒီ အချက်အလက်တွေကို code ထဲမှာပဲ ရေးထားခိုင်းပါတယ်။ ပြီးရင် ကွန်ပျူတာက သင့်အစား မှတ်ထားပေးပြီး မှားတဲ့နေရာကို ချက်ချင်း ပြပေးပါတယ်။ ရလဒ်က bug နည်းလာတာ၊ editor က ပိုကောင်းတဲ့ အကြံပြုချက်တွေ ပေးတာ၊ code ကို ဖတ်ရ ပြင်ရ ပိုလွယ်လာတာ ဆိုတဲ့ အားသာချက်သုံးခုပါ။

Problem → Why → Solution

ပြဿနာ: "100" + 10 = "10010"

HTML input တွေက user ဘာရိုက်ရိုက် string ပဲ ပြန်ပေးပါတယ်။ ဈေးနှုန်းကို form ကနေ ယူပြီး အခွန် ပေါင်းတဲ့ function ထဲ တိုက်ရိုက် ထည့်လိုက်ရင် `*` operator က string ကို number ပြောင်းပေးပေမဲ့ `+` operator ကတော့ စာသားနှစ်ခုကို ဆက်ပေးလိုက်ပါတယ်။

ရလဒ်က `110` မဟုတ်ဘဲ `"10010"` ပါ။ Crash မဖြစ်တဲ့အတွက် ဒီ value က invoice ထဲ၊ database ထဲအထိ ရောက်သွားနိုင်ပါတယ်။ Customer တစ်ယောက်က 10,010 ဒေါ်လာ bill ကို မြင်ပြီး complaint တက်မှ သိရတတ်ပါတယ်။

tax.js
// JavaScript: price is supposed to be a number, but nothing says so
function addTax(price, rate) {
  return price + price * rate;
}

const priceFromForm = "100";            // input.value is always a string
console.log(addTax(priceFromForm, 0.1)); // "10010" instead of 110

ဘာကြောင့် ဒီ bug က ရှာရခက်လဲ

Error message လုံးဝ မရှိပါဘူး။ Bug ဖြစ်တဲ့နေရာ (form ကနေ ဖတ်တဲ့ line) နဲ့ မှားတဲ့ ရလဒ်ကို မြင်ရတဲ့နေရာ (invoice) က file မတူ၊ အချိန်မတူပါဘူး။ TypeScript မှာ `price: number` လို့ ရေးထားရင် call လုပ်တဲ့ line ပေါ်မှာပဲ 'Argument of type string is not assignable to parameter of type number' ဆိုပြီး ပြပေးပါတယ်။

အဓိက သဘောတရား: အားသာချက်သုံးခုနဲ့ သူ့ရဲ့ နယ်နိမိတ်

Bug တစ်ခုကို ပြင်ရတဲ့ ကုန်ကျစရိတ်က bug စဖြစ်တဲ့နေရာနဲ့ တွေ့ရှိတဲ့နေရာ ဘယ်လောက်ဝေးလဲ ဆိုတာပေါ် မူတည်ပါတယ်။ ရိုက်နေတုန်း အနီမျဉ်းနဲ့ တွေ့ရင် စက္ကန့်ပိုင်းပဲ ကြာပါတယ်။ Production မှာ customer က တွေ့မှ ဆိုရင်တော့ log တွေ ရှာ၊ data ပြင်၊ တောင်းပန်ရပါတယ်။ TypeScript ရဲ့ အားသာချက်သုံးခုစလုံးက ဒီအကွာအဝေးကို တိုစေဖို့ ရည်ရွယ်ပါတယ်။

Static type checking

Code ကို run မကြည့်ခင် compile time မှာ type ဆိုင်ရာ error တွေကို ရှာပေးပါတယ်: number variable ထဲ string ရောက်လာတာ၊ မရှိတဲ့ property ကို ဖတ်တာ၊ argument အရေအတွက် မှားတာ၊ `undefined` ဖြစ်နိုင်တာကို မစစ်ဘဲ သုံးတာ စတာတွေပါ။ Test တွေက run တဲ့ လမ်းကြောင်းကိုပဲ စစ်ပေမဲ့ compiler ကတော့ code လမ်းကြောင်း အားလုံးကို စစ်ပါတယ်။

ပိုကောင်းတဲ့ autocomplete

Editor က variable တိုင်းရဲ့ type ကို သိလို့ `user.` လို့ ရိုက်လိုက်တာနဲ့ အဲဒီ object မှာ တကယ်ရှိတဲ့ property တွေကိုပဲ ပြပေးပါတယ်။ Function တစ်ခုကို ခေါ်တဲ့အခါ parameter နာမည်နဲ့ type တွေကို ပြပေးလို့ documentation ကို ခဏခဏ ဖွင့်ကြည့်စရာ မလိုတော့ပါဘူး။

ဖတ်ရလွယ်ပြီး ထိန်းသိမ်းရလွယ်တဲ့ code

`function addTax(price: number, rate: number): number` ဆိုတဲ့ signature က comment တစ်ခုထက် ပိုယုံကြည်ရပါတယ်၊ compiler က အမြဲ စစ်နေလို့ပါ။ Function ကို ပြင်လိုက်ရင် အဲဒီ function ကို ခေါ်ထားတဲ့ နေရာတိုင်းကို compiler က စာရင်းပြပေးတာမို့ project ကြီးတွေကို ယုံကြည်စိတ်ချစွာ ပြင်လို့ရပါတယ်။

အားသာချက်တွေရဲ့ နယ်နိမိတ်ကိုလည်း ရိုးရိုးသားသား သိထားဖို့ လိုပါတယ်။ TypeScript က build step တစ်ခု ထပ်ထည့်ပြီး type တွေ ရေးဖို့ အချိန်နည်းနည်း ပိုယူပါတယ်။ ပိုအရေးကြီးတာက type တွေက runtime မှာ မရှိတော့တာပါ: form, API, file ကလာတဲ့ data တွေက သင်ရေးထားတဲ့ type အတိုင်း ဖြစ်မနေနိုင်ပါဘူး။ ဒါကြောင့် အဲဒီ boundary မှာ data ကို ကိုယ်တိုင် ပြောင်းရ၊ စစ်ရပါတယ်။ 'ဈေးနှုန်းကို 10% လျှော့ရမှာကို 20% လျှော့မိတယ်' လို logic bug တွေကိုလည်း type တွေက မဖမ်းနိုင်ပါဘူး၊ test တွေ လိုအပ်ဆဲပါ။

Bug တစ်ခုကို ဘယ်အချိန် တွေ့လဲ
  1. Editor မှာ ရိုက်နေတုန်း (TS)
  2. tsc / CI မှာ (TS)
  3. Test run တဲ့အချိန်
  4. Production မှာ user ကြုံမှ

Bug တစ်ခုကို စောစော တွေ့လေ ပြင်ရ သက်သာလေပါ။ TypeScript က type bug တွေကို ပထမ အဆင့်နှစ်ဆင့်ဆီ ရွှေ့ပေးပါတယ်။

Boundary မှာ တစ်ကြိမ်ပဲ ပြောင်းပါ

typescript
function work(input: ExpectedType): Result {
  // inside, every value already has the right type
}

const raw = readFromOutside();      // string, unknown data...
const value = convertOrCheck(raw);  // Number(), parse, validate
work(value);

Function တွေရဲ့ parameter type တွေက ကတိပါ။ အပြင်ကလာတဲ့ data ကို boundary မှာ တစ်ကြိမ်တည်း ပြောင်းပြီးမှ ထည့်ပေးပါ။ ဒါဆို အတွင်းပိုင်း code အားလုံးက type ကို ယုံလို့ရပါတယ်။

TypeScript က tax bug ကို ဖမ်းပုံ

Problem ထဲက function ကိုပဲ type ပေးလိုက်ပါတယ်။ Comment ပိတ်ထားတဲ့ call က string ကို တိုက်ရိုက် ထည့်တဲ့အတွက် compiler က ငြင်းပါတယ်။ အောက်က code ကတော့ string ကို boundary မှာ `Number()` နဲ့ number ပြောင်းပြီးမှ ထည့်ပါတယ်။

Run မကြည့်ခင် output နှစ်ကြောင်းကို ကြိုမှန်းကြည့်ပါ။ ပြီးရင် comment ပိတ်ထားတဲ့ call ကို ဖွင့်ပြီး သင့် editor ပြတဲ့ error message ကို example ထဲက message နဲ့ နှိုင်းယှဉ်ကြည့်ပါ။

tax.ts
// Static types turn a silent runtime bug into an editor error.
function addTax(price: number, rate: number): number {
  return price + price * rate;
}

const priceFromForm = "100"; // values from HTML inputs are strings

// addTax(priceFromForm, 0.1);
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.

const price = Number(priceFromForm); // convert once, at the boundary
console.log(addTax(price, 0.1));
console.log(addTax(250, 0.05).toFixed(2));

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

  1. The contractfunction addTax(price: number, rate: number): number {

    `price` နဲ့ `rate` နှစ်ခုလုံး number ဖြစ်ရမယ်၊ ရလဒ်ကလည်း number ဖြစ်ရမယ်။ ဒီ line တစ်ကြောင်းက problem ထဲက bug ကို မဖြစ်နိုင်အောင် လုပ်ပေးပါတယ်။

  2. The rejected call// addTax(priceFromForm, 0.1);

    `priceFromForm` ရဲ့ type ကို TypeScript က string လို့ infer လုပ်ထားလို့ ဒီ call ကို compile error ပေးပါတယ်။ JavaScript မှာတော့ `"10010"` ထွက်မယ့် call ပါ။

  3. Convert at the boundaryconst price = Number(priceFromForm); // convert once, at the boundary

    `Number()` က runtime မှာ value ကို တကယ် ပြောင်းပေးပြီး ရလဒ်ရဲ့ type က number ပါ။ ဒါကြောင့် `addTax(price, 0.1)` က `110` ကို ထုတ်ပါတယ်။

  4. Type-aware autocompleteconsole.log(addTax(250, 0.05).toFixed(2));

    Return type က number ဖြစ်တာကို editor က သိလို့ `.` ရိုက်တာနဲ့ `toFixed` လို number method တွေကို ပြပေးပါတယ်။ `262.5` ကို ဒသမ နှစ်နေရာနဲ့ `262.50` အဖြစ် ထုတ်ပါတယ်။

You should see
110
262.50

TypeScript က ဘယ်အချိန်မှာ အများဆုံး တန်ဖိုးရှိလဲ

Project တိုင်းအတွက် အကျိုးအမြတ် တူညီတာ မဟုတ်ပါဘူး။ အောက်က ဇယားက project အမျိုးအစားအလိုက် TypeScript ပေးတဲ့ တန်ဖိုးကို နှိုင်းယှဉ်ပြထားပါတယ်။ ယေဘုယျ စည်းမျဉ်းကတော့ code က ကြာကြာ အသုံးပြုမယ်၊ လူအများ ထိမယ်ဆိုရင် TypeScript က ပိုတန်ဖိုးရှိပါတယ်။

Project အမျိုးအစားTypeScript ရဲ့ တန်ဖိုး
တစ်ခါသုံး script ဆယ်ကြောင်းနည်းပါတယ်၊ JavaScript လည်း ရပါတယ်
လနဲ့ချီ ပြင်ရမယ့် app တစ်ခုမြင့်ပါတယ်၊ refactor တိုင်း လုံခြုံစေပါတယ်
အဖွဲ့ကြီးတစ်ခုရဲ့ shared codeအလွန်မြင့်ပါတယ်၊ type တွေက အဖွဲ့ကြား ကတိဖြစ်ပါတယ်
အခြားသူတွေ သုံးမယ့် libraryအလွန်မြင့်ပါတယ်၊ သုံးသူတွေ autocomplete ရပါတယ်

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

အဖွဲ့တွေက TypeScript ကို ဘာကြောင့် ရွေးကြလဲ

Developer အများအပြား အတူတူ အလုပ်လုပ်တဲ့ project တွေမှာ TypeScript ရဲ့ တန်ဖိုးက ပိုထင်ရှားပါတယ်။ Backend team က API response ထဲက `price` ကို `amount` လို့ နာမည်ပြောင်းလိုက်ရင် frontend ရဲ့ shared type ကို update လုပ်လိုက်တာနဲ့ အဲဒီ field ကို သုံးထားတဲ့ component တိုင်းက compile error ပြပါတယ်။ Production မှာ `undefined` တွေ ပေါ်လာမှ ရှာနေစရာ မလိုတော့ပါဘူး။

အဖွဲ့ထဲ လူသစ်ဝင်လာတဲ့အခါမှာလည်း ကူညီပါတယ်။ Function signature တွေနဲ့ autocomplete က codebase ကို လမ်းပြပေးလို့ ဘယ် object မှာ ဘာ field တွေ ရှိလဲ ဆိုတာ မေးနေစရာ မလိုပါဘူး။ ဒါပေမဲ့ form, API, database ကလာတဲ့ data တွေကိုတော့ boundary မှာ ပြောင်း၊ စစ်ပြီးမှ type ပေးပါ၊ Advanced အပိုင်းမှာ zod နဲ့ ဒီအလုပ်ကို လုပ်ပုံ လေ့လာရပါမယ်။ လူကြိုက်များတဲ့ open-source library အများအပြားကလည်း ဒီအကြောင်းရင်းတွေကြောင့်ပဲ TypeScript နဲ့ ရေးထားတာ ဒါမှမဟုတ် type definition တွေ ထည့်ပေးထားတာပါ။

အမှားများ

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

`as` နဲ့ string ကို number "ပြောင်း" တာ

`addTax(priceFromForm as unknown as number, 0.1)` က compile error ကို ဖျောက်ပေးပေမဲ့ value ကို ဘာမှ မပြောင်းပါဘူး။ `as` က compiler ကို 'ငါ့ကို ယုံ' လို့ ပြောတာသက်သက်ပါ၊ runtime မှာ string ပဲ ကျန်နေပြီး `"10010"` bug ပြန်ရောက်လာပါတယ်။ တကယ်ပြောင်းဖို့ `Number()` ဒါမှမဟုတ် `parseFloat()` ကို သုံးပြီး `NaN` ဖြစ်မဖြစ်ကိုလည်း စစ်ပါ။

Type check အောင်ရင် test မလိုဘူးလို့ ထင်တာ

`addTax` ထဲမှာ `price - price * rate` လို့ လက္ခဏာ မှားရေးမိရင်တောင် type အားလုံး မှန်နေတာမို့ compiler က ဘာမှ မပြောပါဘူး။ Type တွေက value ရဲ့ ပုံသဏ္ဌာန်ကို စစ်တာ၊ တွက်ချက်မှု မှန်မမှန်ကို မစစ်ပါဘူး။ `addTax(100, 0.1) === 110` လို test တစ်ခု လိုအပ်ဆဲပါ။

`any` နဲ့ ပြည့်နေတဲ့ code ကနေ အကျိုးကျေးဇူး မျှော်လင့်တာ

`any` လို့ type ပေးထားတဲ့ value မှာ compiler က ဘာမှ မစစ်တော့သလို editor ကလည်း autocomplete မပေးနိုင်တော့ပါဘူး။ `any` များတဲ့ project က build step ရဲ့ ကုန်ကျစရိတ်ကို ပေးရပေမဲ့ အားသာချက်တွေကို မရပါဘူး။

Mini challenge

Mini challenge: order line တစ်ကြောင်း

ပစ္စည်းနာမည်၊ unit price နဲ့ quantity ကို ယူပြီး `3 x Notebook = 7.50` လိုမျိုး စာကြောင်း ပြန်ပေးတဲ့ typed function တွေ ရေးပါ။ Quantity တစ်ခုက form ကနေ string အနေနဲ့ ရောက်လာပါတယ်။

  • `lineTotal(unitPrice, quantity)` က number ပြန်ပေးရမယ်
  • `formatLine(name, unitPrice, quantity)` က string ပြန်ပေးပြီး total ကို ဒသမ နှစ်နေရာ ပြရမယ်
  • Form ကလာတဲ့ `"3"` ကို `as` မသုံးဘဲ boundary မှာ ပြောင်းပါ
order-line.ts
// TODO: annotate both parameters and the return type
function lineTotal(unitPrice, quantity) {
  // TODO: multiply
}

// TODO: name is a string; unitPrice and quantity are numbers; returns a string
function formatLine(name, unitPrice, quantity) {
  // TODO: "<quantity> x <name> = <total with 2 decimals>"
}

const quantityFromForm = "3";
// TODO: convert quantityFromForm, then print Notebook (2.5) and Pen (0.75 x 4)

ရလဒ်: `3 x Notebook = 7.50` နဲ့ `4 x Pen = 3.00` ကို print ထုတ်ပါမယ်။

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

`toFixed(2)` က number method ပါ၊ string ကို ပြန်ပေးပါတယ်။ `formatLine` ရဲ့ return type နဲ့ ကိုက်ပါတယ်။

အဖြေ ကြည့်ရန်
order-line.ts
function lineTotal(unitPrice: number, quantity: number): number {
  return unitPrice * quantity;
}

function formatLine(name: string, unitPrice: number, quantity: number): string {
  const total = lineTotal(unitPrice, quantity).toFixed(2);
  return `${quantity} x ${name} = ${total}`;
}

const quantityFromForm = "3";
const quantity = Number(quantityFromForm);

console.log(formatLine("Notebook", 2.5, quantity));
console.log(formatLine("Pen", 0.75, 4));

Quick Quiz 1 / 3

Example ထဲမှာ `const price = Number(priceFromForm);` ပြီးနောက် `console.log(addTax(price, 0.1))` က ဘာ print ထုတ်လဲ?

Quick Quiz 2 / 3

Form ကလာတဲ့ `priceFromForm` (string) ကို `addTax` ထဲ ထည့်ဖို့ ဘယ်နည်းက မှန်လဲ?

Quick Quiz 3 / 3

TypeScript ရဲ့ အားသာချက်တွေအကြောင်း ဘယ်စာကြောင်းက မှားလဲ?

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

  • Static type checking က string/number ရောတာလို bug တွေကို run မတိုင်ခင် ဖမ်းပေးပါတယ်
  • Type ကို သိတဲ့ editor က ပိုတိကျတဲ့ autocomplete နဲ့ signature help ပေးပါတယ်
  • Signature တွေက ယုံကြည်ရတဲ့ documentation ဖြစ်ပြီး refactor ကို လုံခြုံစေပါတယ်
  • အပြင်က data ကို boundary မှာ `Number()` စတာနဲ့ တကယ်ပြောင်းပါ၊ `as` နဲ့ မလှည့်စားပါနဲ့
  • Type တွေက logic bug ကို မဖမ်းနိုင်လို့ test လိုအပ်ဆဲပါ

Announcing TypeScript 7.0 — TypeScript

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

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

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

Why Use TypeScript? | Thuta Learning