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

Enums

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

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

  • Numeric enum နဲ့ string enum ကို declare လုပ်ပြီး member တစ်ခုစီရဲ့ value ကို ကြိုတင် ခန့်မှန်းနိုင်ဖို့
  • Type အများစုနဲ့ မတူဘဲ enum က runtime မှာ object အစစ်တစ်ခုအဖြစ် ဘာကြောင့် ကျန်နေလဲ ရှင်းပြနိုင်ဖို့
  • Numeric enum ကို loop ပတ်တဲ့အခါ reverse mapping ကြောင့် ဖြစ်တဲ့ ထောင်ချောက်ကို ရှောင်နိုင်ဖို့
  • Enum, string-literal union နဲ့ as const object သုံးမျိုးထဲက လိုအပ်ချက်နဲ့ ကိုက်တာကို ရွေးနိုင်ဖို့

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

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

Program ထဲက value တချို့က တိုတိုနဲ့ ပုံသေ စာရင်းထဲက တစ်ခုပဲ ဖြစ်ခွင့်ရှိပါတယ်။ Game ထဲက player က Up, Down, Left, Right ဆိုတဲ့ ဘက်လေးဘက်ထဲက တစ်ဘက်ကိုပဲ သွားနိုင်တယ်၊ order တစ်ခုက Pending, Paid, Shipped ထဲက တစ်ခုပဲ ဖြစ်နိုင်တယ် ဆိုပါစို့။ အဲဒါတွေကို ပြန့်ကျဲနေတဲ့ string တွေ ဒါမှမဟုတ် 0 နဲ့ 3 လို magic number တွေနဲ့ ရေးရင် ဘယ်သူမှ မမြင်တဲ့ typo တွေ ဝင်လာနိုင်ပါတယ်။

Enum က အဲဒီ စာရင်းတစ်ခုလုံးကို နာမည်တစ်ခု ပေးပြီး option တစ်ခုစီကိုလည်း ဖတ်ရလွယ်တဲ့ label တစ်ခုစီ ပေးပါတယ်။ ဒါကြောင့် 0 က အပေါ်ဘက်လို့ မှတ်နေစရာ မလိုတော့ဘဲ Direction.Up လို့ ရေးလိုက်ရုံပါပဲ။ Compiler ကလည်း စာရင်းထဲမှာ တကယ်ရှိတဲ့ label တွေကိုပဲ သုံးခွင့်ပေးပြီး editor က autocomplete လုပ်ပေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: comment ထဲမှာပဲ ရှိနေတဲ့ စည်းမျဉ်း

Game သေးသေးလေးတစ်ခုက player ရဲ့ direction ကို number အဖြစ် သိမ်းထားတယ် ဆိုပါစို့: 0 က အပေါ်၊ 1 က အောက်၊ 2 က ဘယ်၊ 3 က ညာ ပါ။ ဒီစည်းမျဉ်းက comment တစ်ကြောင်းထဲမှာပဲ ရှိနေပါတယ်။ လအနည်းငယ်ကြာတော့ တစ်ယောက်ယောက်က move(4) လို့ ရေးမိတယ်၊ ဒါမှမဟုတ် အပေါ်ကို ဆိုလိုပြီး 1 နဲ့ နှိုင်းယှဉ်မိတယ်။ နှစ်ခုလုံးက valid number တွေ ဖြစ်တာကြောင့် TypeScript က ဘာမှ မပြောပါဘူး။ Bug က character မလှုပ်တော့တဲ့ ပုံစံနဲ့ပဲ ပေါ်လာပါတယ်။

magic-numbers.ts
// 0 = up, 1 = down, 2 = left, 3 = right ... hopefully everyone remembers
function move(direction: number) {
  if (direction === 0) console.log("Moving up!");
  if (direction === 1) console.log("Moving up!"); // wrong number, no error
}

move(4); // no such direction, still compiles

ဘာကြောင့် compiler က မကူညီနိုင်တာလဲ

number ဆိုတဲ့ type က value ကို ဘယ်လို သိမ်းထားလဲ ဆိုတာကိုပဲ ပြောပြီး အဓိပ္ပာယ် ဘာလဲ ဆိုတာကို မပြောပါဘူး။ Number ထောင်ပေါင်းများစွာက valid number တွေ ဖြစ်ပေမယ့် valid direction ကတော့ လေးခုပဲ ရှိပါတယ်။ ခွင့်ပြုတဲ့ value အစုက option စာရင်းနဲ့ အတိအကျ တူပြီး လူဖတ်လို့ နားလည်တဲ့ နာမည်တွေပါတဲ့ type တစ်ခု လိုအပ်ပါတယ်။

အဓိက concept: type ရော value ရော ဖြစ်တဲ့ declaration

Enum declaration တစ်ခုက အလုပ်နှစ်ခုကို တစ်ပြိုင်နက် လုပ်ပါတယ်။ ပထမ — Direction ဆိုတဲ့ type တစ်ခု ဆောက်ပေးပြီး အဲဒီ type မှာ ခွင့်ပြုတဲ့ value တွေက သူ့ member တွေပဲ ဖြစ်ပါတယ်။ ဒုတိယ — Direction လို့ပဲ နာမည်ရတဲ့ JavaScript object အစစ်တစ်ခုကိုလည်း ဆောက်ပေးပြီး runtime မှာ အဲဒီ value တွေကို သိမ်းထားပါတယ်။ ဒုတိယအချက်က ထူးခြားပါတယ်: type alias နဲ့ interface လို TypeScript မှာ ရေးတဲ့ အရာအများစုက compile လုပ်ပြီးရင် ပျောက်သွားပေမယ့် enum ကတော့ code အဖြစ် ထွက်လာပါတယ်။

Numeric enum မှာ ပထမ member က default အားဖြင့် 0 ဖြစ်ပြီး နောက်က member တစ်ခုစီက ရှေ့ member ထက် တစ်ပိုပါတယ်။ ဒါကြောင့် Up က 0၊ Down က 1၊ Left က 2၊ Right က 3 ဖြစ်ပါတယ်။ စမယ့် value ကို Up = 1 လို သတ်မှတ်လို့ ရသလို member တိုင်းကို number တစ်ခုစီ တိုက်ရိုက် ပေးလို့လည်း ရပါတယ်။ String enum မှာတော့ member တိုင်းကို Paid = "PAID" လို string တစ်ခုနဲ့ initialise လုပ်ရပါမယ်။ Log line မှာ 1 ဆိုတဲ့ number အလွတ်အစား "PAID" ကို မြင်ရတာကြောင့် string enum က debug လုပ်ရ ပိုလွယ်ပါတယ်။

အချက်Numeric enumString enum
Default value0, 1, 2... အလိုအလျောက် တိုးသွားတယ်မရှိပါ၊ member တိုင်း string လိုတယ်
Log/JSON ထဲမှာ2 လို number တစ်ခု"LEFT" လို ဖတ်ရလွယ်တဲ့ စာ
Reverse mappingရှိတယ်: Direction[2] က "Left"မရှိပါ

Reverse mapping က လူအများ အံ့သြတတ်တဲ့ အပိုင်းပါ။ Numeric enum ကို compile လုပ်လိုက်တဲ့ object က နာမည်ကနေ number ကို map လုပ်ရုံသာမက number ကနေ နာမည်ကိုလည်း ပြန် map လုပ်ထားပါတယ်။ ဒါကြောင့် Object.keys(Direction) က key လေးခု မဟုတ်ဘဲ ရှစ်ခု ပြန်ပေးတာပါ။ String enum ကတော့ နာမည်ကနေ value ကိုပဲ map လုပ်ပါတယ်။

text
What enum Direction { Up, Down } compiles to
--------------------------------------------
TypeScript source              JavaScript object at runtime
-----------------              ----------------------------
enum Direction {               {
  Up,      // 0                  "0": "Up",     Up: 0,
  Down,    // 1                  "1": "Down",   Down: 1
}                              }
                               names -> numbers AND numbers -> names

နောက်ဆုံးအနေနဲ့ ပုံသေ စာရင်းတစ်ခုကို model လုပ်ဖို့ enum တစ်မျိုးတည်း မဟုတ်ပါဘူး။ type Direction = "up" | "down" လို string-literal union က runtime code လုံးဝ မထုတ်ဘဲ compile-time safety တူတူ ပေးပါတယ်။ as const object ကတော့ runtime object ရော type ရော နှစ်ခုလုံး ပေးပါတယ်။ Type ကိုပဲ ဖယ်ထုတ်ပြီး code ကို မပြောင်းပေးတဲ့ tool တွေ သုံးတဲ့အခါ အထူးသဖြင့် team အများအပြားက အဲဒီ နည်းလမ်းတွေကို ပိုကြိုက်ကြပါတယ်။ ဒီအကြောင်းကို အောက်မှာ ဆက်ကြည့်ပါမယ်။

မှတ်ထားရမယ့် ပုံစံ
  1. စာရင်းကို တစ်ခါပဲ ကြေညာ: enum Direction
  2. Member တွေ value ရ: 0,1,2 ဒါမှမဟုတ် string
  3. Type: member တွေကိုပဲ ခွင့်ပြု
  4. Runtime: object အစစ် ထွက်လာ

Enum က type ရော value ရော ဖြစ်ပါတယ် — type alias နဲ့ ကွာခြားတာ အဲဒီအချက်ပါပဲ။

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

typescript
enum Name {
  First,          // 0
  Second,         // 1
}

enum Label {
  A = "A_VALUE",  // string members need explicit values
  B = "B_VALUE",
}

let current: Name = Name.First;

Numeric member တွေက value မပေးရင် 0 ကနေ စရေတွက်ပါတယ်။ String member တွေကတော့ တစ်ခုချင်းစီ initialise လုပ်ရပါမယ်။ Enum နာမည်ကို type အဖြစ်သုံးပြီး Name.Member ကို value အဖြစ် သုံးပါတယ်။

Enum နဲ့ player ကို ရွှေ့ကြည့်ရအောင်

အောက်က example က game ရဲ့ direction လေးခုကို declare လုပ်ပြီး player ရဲ့ လက်ရှိ direction ကို enum type ပါတဲ့ variable တစ်ခုထဲ သိမ်းကာ member တစ်ခုနဲ့ နှိုင်းယှဉ်ပါတယ်။

typescript
enum Direction {
  Up,
  Down,
  Left,
  Right,
}

let playerDirection: Direction = Direction.Up;

if (playerDirection === Direction.Up) {
  console.log("Moving up!");
}

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

  1. Lines 1-6enum Direction {

    Member လေးခုပါတဲ့ numeric enum တစ်ခုကို ကြေညာပါတယ်။ ဘယ် member မှာမှ initialiser မပါတာကြောင့် Up က 0၊ Down က 1၊ Left က 2၊ Right က 3 ဖြစ်ပါတယ်။

  2. Line 8let playerDirection: Direction = Direction.Up;

    Direction ကို နှစ်ခါ သုံးထားပါတယ်: ပထမတစ်ခါက type annotation အဖြစ်၊ ဒုတိယတစ်ခါက runtime object ထဲက value ကို ယူတဲ့ value အဖြစ်ပါ။

  3. Line 10if (playerDirection === Direction.Up) {

    Runtime မှာ ဒါက number နှစ်ခု နှိုင်းယှဉ်တာပါပဲ (0 === 0)။ Direction.Upp လို စာလုံးပေါင်းမှားရင်တော့ compile error တက်ပါတယ်။

  4. Line 11console.log("Moving up!");

    Condition က true ဖြစ်တာကြောင့် ဒီလိုင်း run ပြီး Moving up! ကို print ထုတ်ပါတယ်။

You should see
Moving up!

Enum, union နဲ့ as const object ကို နှိုင်းယှဉ်ကြည့်ရအောင်

ပုံသေ value စာရင်း တစ်ခုတည်းကို နည်းလမ်းသုံးမျိုးနဲ့ ရေးကြည့်ပါမယ်။ အဓိက ကွာခြားချက်က string enum type ထဲကို "PAID" ဆိုတဲ့ string literal အလွတ်ကို ထည့်လို့ မရဘဲ Status.Paid လို့ ရေးရတာပါ။ Union ကတော့ "PAID" ကို တိုက်ရိုက် လက်ခံပါတယ်။

three-ways.ts
// 1. Enum: a type AND a runtime object
enum Status { Pending = "PENDING", Paid = "PAID" }

// 2. String-literal union: a type only, erased when compiled
type StatusUnion = "PENDING" | "PAID";

// 3. as const object: a runtime object plus a type derived from it
const STATUS = { Pending: "PENDING", Paid: "PAID" } as const;
type StatusValue = (typeof STATUS)[keyof typeof STATUS]; // "PENDING" | "PAID"

const a: Status = Status.Paid;    // const x: Status = "PAID" would be an error
const b: StatusUnion = "PAID";
const c: StatusValue = STATUS.Paid;
console.log(a, b, c); // PAID PAID PAID
နည်းလမ်းဘယ်အချိန် သင့်တော်လဲ
enumCode base က enum ကို သုံးပြီးသားဖြစ်ပြီး build tool က enum ကို transform လုပ်ပေးနိုင်တဲ့အခါ
String-literal unionType safety ပဲ လိုပြီး runtime မှာ value စာရင်း မလိုတဲ့အခါ
as const objectLoop ပတ်ဖို့ ဒါမှမဟုတ် validate လုပ်ဖို့ runtime စာရင်းရော type ရော လိုတဲ့အခါ

Type ကိုပဲ ဖယ်ထုတ်တဲ့ tool တွေနဲ့ enum

Enum က runtime code ထုတ်ပေးရတာကြောင့် type annotation တွေကိုပဲ ဖျက်ပစ်တဲ့ tool (ဥပမာ Node.js ရဲ့ built-in TypeScript support) တွေက enum ကို default အားဖြင့် run မပေးနိုင်ပါဘူး။ TypeScript 5.8 က ထည့်ပေးခဲ့တဲ့ erasableSyntaxOnly option ကို ဖွင့်ထားရင် compiler က enum ကို error အဖြစ် ပြပေးပါတယ်။ const enum ကလည်း value ကို inline လုပ်တာကြောင့် file တစ်ခုချင်းစီ compile လုပ်တဲ့ isolatedModules setup တွေမှာ ပြဿနာ ဖြစ်တတ်ပါတယ်။

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

လက်တွေ့ project: system boundary ကို ဖြတ်သွားတဲ့ status value

လက်တွေ့ project တွေမှာ enum value တွေက code ထဲမှာပဲ မနေပါဘူး။ Order status က database column ထဲ သိမ်းခံရတယ်၊ API response ထဲ ပါသွားတယ်၊ analytics event ထဲ log ဖြစ်တယ်။ အဲဒီလို boundary ကို ဖြတ်သွားတဲ့ value တွေအတွက် string enum (ဒါမှမဟုတ် string union) ကို သုံးပါ၊ ဒါမှ data ကို ဖတ်တဲ့ သူတိုင်းက 2 အစား "SHIPPED" ကို မြင်ရပြီး member အစီအစဉ် ပြောင်းသွားရင်လည်း stored data က အဓိပ္ပာယ် မပြောင်းပါဘူး။

ဒါပေမယ့် request body ထဲက string က enum type ကို ကိုယ်တိုင် မစစ်ပေးပါဘူး — JSON ထဲက ဘာမဆို ဝင်လာနိုင်ပါတယ်။ Enum ရဲ့ runtime object ကို သုံးပြီး ဝင်လာတဲ့ value ကို boundary မှာ စစ်ပါ:

parse-status.ts
enum OrderStatus { Pending = "PENDING", Paid = "PAID", Shipped = "SHIPPED" }

const allowed: string[] = Object.values(OrderStatus);

function parseStatus(raw: string): OrderStatus | undefined {
  return allowed.includes(raw) ? (raw as OrderStatus) : undefined;
}

console.log(parseStatus("PAID"));     // PAID
console.log(parseStatus("REFUNDED")); // undefined

ဒီ pattern ကို admin panel ထဲက dropdown option တွေ ထုတ်တာ၊ feature flag နာမည်တွေ၊ user role (Admin, Editor, Viewer) တွေ စတဲ့ နေရာတွေမှာ မကြာခဏ တွေ့ရပါတယ်။

အမှားများ

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

1. Numeric enum ကို loop ပတ်ပြီး number တွေပါ ရလာတာ

Reverse mapping ကြောင့် Object.keys(Direction) က '0', '1', '2', '3', 'Up', 'Down', 'Left', 'Right' ဆိုပြီး ရှစ်ခု ပြန်ပေးပါတယ်။ Dropdown တစ်ခုကို အဲဒါနဲ့ ဆောက်ရင် option ရှစ်ခု ထွက်လာပါမယ်။ isNaN(Number(key)) နဲ့ number key တွေကို filter လုပ်ပါ၊ ဒါမှမဟုတ် reverse mapping မရှိတဲ့ string enum ကို သုံးပါ။

2. Numeric value တွေ သိမ်းထားပြီးမှ member အစီအစဉ် ပြောင်းတာ

Database ထဲမှာ 2 (Left) ကို သိမ်းထားပြီးမှ Up နဲ့ Down ကြားမှာ Stop ဆိုတဲ့ member အသစ် ထည့်လိုက်ရင် နောက်က member တွေရဲ့ number တွေ အားလုံး ရွေ့သွားပါတယ်။ သိမ်းထားတဲ့ 2 က အခု Down ကို ဆိုလိုသွားပြီး error တစ်ခုမှ မတက်ပါဘူး။ သိမ်းထားမယ့် value တွေကို explicit number ဒါမှမဟုတ် string နဲ့ ပေးပါ။

3. Numeric enum type က မှားတဲ့ number ကို အမြဲ တားမယ်လို့ ထင်တာ

let d: Direction = 7 လို literal ကိုတော့ compiler က တားပါတယ်။ ဒါပေမယ့် number type ရှိတဲ့ variable တစ်ခုကိုတော့ numeric enum ထဲ assign လုပ်ခွင့်ပြုပါတယ်။ ဒါကြောင့် JSON ထဲက ဒါမှမဟုတ် calculation ကနေ ရလာတဲ့ 99 က Direction type နဲ့ ဖြတ်သွားနိုင်ပါတယ်။ ပြင်ပ value တွေကို runtime မှာ စစ်ပါ။

Best Practices

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

  • Log, database ဒါမှမဟုတ် API ထဲ ရောက်သွားမယ့် value တွေအတွက် string enum ဒါမှမဟုတ် string union ကို ရွေးပါ။
  • Numeric enum ကို ဆက်သုံးမယ်ဆိုရင် persisted member တိုင်းကို explicit number ပေးပါ။
  • Runtime စာရင်း မလိုရင် enum အစား string-literal union ကို စဉ်းစားပါ။
  • ပြင်ပကလာတဲ့ value ကို Object.values(TheEnum) နဲ့ boundary မှာ စစ်ပါ။

Mini challenge

Log-level filter တစ်ခု

ကိုယ်တိုင် ရွေးထားတဲ့ value တွေပါတဲ့ numeric enum နဲ့ log level တွေကို model လုပ်ပြီး message တစ်ခုက print ထုတ်လောက်အောင် အရေးကြီးလား ဆုံးဖြတ်တဲ့ function တစ်ခု ရေးပါ။

  • Debug = 10, Info = 20, Warn = 30, Error = 40 ပါတဲ့ enum LogLevel ကို ကြေညာပါ။
  • shouldLog(level, minimum) က level က minimum ထက် မငယ်ရင် true ပြန်ပေးရပါမယ်။
  • log(level, message, minimum) က reverse mapping LogLevel[level] ကို သုံးပြီး "[WARN] message" ပုံစံ line ကို print ထုတ်ရပါမယ်။
  • minimum ကို LogLevel.Warn ထားရင် Warn နဲ့ Error message တွေပဲ ထွက်ရပါမယ်။
log-level.ts
// TODO: declare enum LogLevel with Debug = 10, Info = 20, Warn = 30, Error = 40

function shouldLog(level: LogLevel, minimum: LogLevel): boolean {
  // TODO: compare the two numeric values
  return false;
}

function log(level: LogLevel, message: string, minimum: LogLevel): void {
  // TODO: return early unless shouldLog(level, minimum) is true
  // TODO: read the member name with LogLevel[level] and print "[NAME] message"
}

// TODO: call log() once per level, all with minimum = LogLevel.Warn

ရလဒ်: [WARN] Disk is 91% full နဲ့ [ERROR] Payment service timed out ဆိုတဲ့ line နှစ်ကြောင်းပဲ ထွက်ပါမယ်။

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

Numeric enum member တွေက runtime မှာ number တွေ ဖြစ်တာကြောင့် >= နဲ့ တိုက်ရိုက် နှိုင်းယှဉ်လို့ ရပါတယ်။ LogLevel[30] က "Warn" ကို ပြန်ပေးပါတယ်။

အဖြေ ကြည့်ရန်
log-level.ts
enum LogLevel {
  Debug = 10,
  Info = 20,
  Warn = 30,
  Error = 40,
}

function shouldLog(level: LogLevel, minimum: LogLevel): boolean {
  return level >= minimum;
}

function log(level: LogLevel, message: string, minimum: LogLevel): void {
  if (!shouldLog(level, minimum)) return;
  const name = LogLevel[level].toUpperCase();
  console.log(`[${name}] ${message}`);
}

const minimum = LogLevel.Warn;
log(LogLevel.Debug, "Cache miss for user 42", minimum);
log(LogLevel.Info, "Server started on port 3000", minimum);
log(LogLevel.Warn, "Disk is 91% full", minimum);
log(LogLevel.Error, "Payment service timed out", minimum);

Quick Quiz 1 / 3

enum Size { Small, Medium = 5, Large } မှာ Size.Large ရဲ့ value က ဘာလဲ?

Quick Quiz 2 / 3

enum Status { Paid = "PAID" } ရှိတယ်ဆိုရင် ဘယ်လိုင်းက compile error ဖြစ်မလဲ?

Quick Quiz 3 / 3

enum Direction { Up, Down } အတွက် Object.keys(Direction) က key လေးခု ဘာကြောင့် ပြန်ပေးတာလဲ?

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

  • Enum က ပုံသေ စာရင်းကို နာမည်ပေးပြီး type ရော runtime object ရော ဆောက်ပေးပါတယ်။
  • Numeric member တွေက 0 ကနေ တိုးသွားပြီး reverse mapping ရှိပါတယ်၊ string member တွေမှာ မရှိပါဘူး။
  • Boundary ကို ဖြတ်သွားမယ့် value တွေအတွက် string value တွေကို ရွေးပြီး runtime မှာ စစ်ပါ။
  • Runtime code မလိုရင် string-literal union ဒါမှမဟုတ် as const object က ပိုရိုးရှင်းတဲ့ ရွေးချယ်မှု ဖြစ်နိုင်ပါတယ်။

TypeScript Handbook — TypeScript

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

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

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

Enums | Thuta Learning