ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဒီ lesson ကနေစပြီး lesson သုံးခုဆက်တိုက်မှာ terminal ထဲမှာ run တဲ့ Task Manager သေးသေးလေးတစ်ခုကို ဆောက်သွားပါမယ်။ Part 1 မှာ feature တွေ မရေးသေးပါဘူး။ ပထမဆုံး "task တစ်ခုဆိုတာ ဘာတွေပါတာလဲ" ဆိုတဲ့ data shape ကို သေချာ ဆုံးဖြတ်ပါမယ်။
စာရွက် form တစ်ရွက်ကို စဉ်းစားကြည့်ပါ။ နာမည်ရေးဖို့ box တစ်ခု၊ Status အတွက် "Todo / In progress / Done" ဆိုတဲ့ checkbox သုံးခု ပါတယ်ဆိုပါစို့။ Checkbox ရှိနေရင် ဘယ်သူမှ "ပြီးသလောက်ပဲ" လို့ စိတ်ကြိုက် ရေးလို့ မရတော့ပါဘူး။ TypeScript မှာ interface က form ဖြစ်ပြီး enum နဲ့ union type တွေက checkbox တွေပါ။ Form ကို အရင်ဆွဲထားရင် နောက်ပိုင်း Part 2 နဲ့ Part 3 မှာ ထည့်မယ့် code တိုင်းက ဒီ shape ကို compiler က စစ်ပေးပါလိမ့်မယ်။
Problem → Why → Solution
ပြဿနာ: shape မရှိတဲ့ task list
Type မသတ်မှတ်ဘဲ မြန်မြန်ရေးလိုက်တဲ့ ပထမ draft ကို ကြည့်ပါ။ Array ကို any[] လို့ ကြေညာထားလို့ object တိုင်းကို လက်ခံပါတယ်။ ဒုတိယ task မှာ status ကို "Done" (D အကြီး) လို့ ရေးမိပြီး priority ဆိုတဲ့ field နာမည်ကိုလည်း "priorty" လို့ စာလုံးပေါင်းမှားသွားပါတယ်။
ဘာ error မှ မပေါ်ပါဘူး။ ပြီးသွားတဲ့ task တွေကို ရှာတဲ့အခါ "done" (d အသေး) နဲ့ နှိုင်းယှဉ်လို့ 0 ပဲ ပြန်ရပါတယ်။ User က task ကို ပြီးအောင်လုပ်ခဲ့ပေမယ့် app ကတော့ ဘာမှမပြီးသေးဘူးလို့ ပြနေမှာပါ။ ဒီလို bug မျိုးက ရက်သတ္တပတ်တွေကြာမှ သတိထားမိတတ်ပါတယ်။
const tasks: any[] = [];
tasks.push({ id: 1, title: "Write README", status: "todo", priority: "high" });
tasks.push({ id: 2, title: "Fix login bug", status: "Done", priorty: "hgh" });
const done = tasks.filter((t) => t.status === "done");
console.log(done.length); // 0 - nobody noticed "Done" or "priorty"ဘာကြောင့် compiler က မတားတာလဲ
any က "ဒီ value ကို မစစ်နဲ့" လို့ compiler ကို ပြောလိုက်တာပါ။ Task ရဲ့ shape ကို ဘယ်နေရာမှာမှ မရေးထားရင် TypeScript က မှန်/မှား ခွဲဖို့ နှိုင်းယှဉ်စရာ မရှိပါဘူး။ Interface နဲ့ value set ကန့်သတ်ချက်တွေ ရေးထားမှသာ typo တွေကို run မလုပ်ခင် compile time မှာ ဖမ်းနိုင်ပါတယ်။
Core concept: data ကို အရင် design လုပ်ပါ
Project အသစ်တစ်ခု စတဲ့အခါ function တွေကို တန်းမရေးဘဲ "ဒီ app က ဘယ် data ကို ကိုင်တွယ်မှာလဲ" ဆိုတာကို type အနေနဲ့ အရင်ရေးပါ။ Type တွေက project တစ်ခုလုံးရဲ့ စာချုပ်ပါ။ Function အသစ်တိုင်း၊ feature အသစ်တိုင်းက ဒီစာချုပ်ကို လိုက်နာရပါတယ်။ ဒီ Task Manager အတွက် အဆင့်လေးဆင့်နဲ့ ဆုံးဖြတ်သွားပါမယ်။
၁။ Field တွေနဲ့ type တွေကို စာရင်းလုပ်ပါ
Task တစ်ခုမှာ id (number)၊ title (string)၊ status နဲ့ priority ပါပါတယ်။ ဒါတွေကို interface Task ထဲမှာ ရေးလိုက်တာနဲ့ field မပါတဲ့ object ဒါမှမဟုတ် နာမည်မှားတဲ့ field ပါတဲ့ object literal ကို compiler က ငြင်းပါလိမ့်မယ်။
၂။ ပိတ်ထားတဲ့ value set တွေကို ကန့်သတ်ပါ
Status နဲ့ priority က ဘယ် string မဆို ဖြစ်လို့မရပါဘူး၊ ရွေးချယ်စရာ အနည်းငယ်ပဲ ရှိပါတယ်။ Status ကို string enum နဲ့ ရေးပါတယ်။ Enum ဟာ runtime မှာ TaskStatus ဆိုတဲ့ object တစ်ခုအဖြစ် တကယ်ရှိနေလို့ နောက်ပိုင်း menu တစ်ခုအတွက် Object.values(TaskStatus) နဲ့ loop ပတ်နိုင်ပါတယ်။ "TODO" လို string value တွေပေးထားလို့ log ထဲမှာ 0၊ 1၊ 2 မဟုတ်ဘဲ ဖတ်လို့ရတဲ့ စာသားပေါ်ပါတယ်။ Priority ကိုတော့ literal union နဲ့ ရေးပါတယ်။ ပိုပေါ့ပါးပြီး "high" လို string ကို တိုက်ရိုက် ရေးလို့ရပါတယ်။
၃။ Type ကို ထပ်မရေးဘဲ ပြန်သုံးပါ
addTask() ရဲ့ parameter မှာ "low" | "medium" | "high" ကို ထပ်မရေးဘဲ Task["priority"] လို့ ရေးပါတယ်။ ဒါကို indexed access type လို့ ခေါ်ပါတယ်။ နောက်ပိုင်း "urgent" ကို interface ထဲ ထပ်ထည့်ရင် function ကပါ အလိုလို လိုက်ပြောင်းသွားပါတယ်။
၄။ State ကို typed array ထဲ သိမ်းပါ
const tasks: Task[] = [] ဆိုတာ variable ကို နောက်တစ်ခါ reassign မလုပ်ရဘူးလို့ ပြောတာပါ။ push() နဲ့ ထည့်တာကတော့ ရပါတယ်။ nextId ကတော့ task တစ်ခုထည့်တိုင်း တိုးရလို့ let ဖြစ်ရပါတယ်။
| String enum (status) | Literal union (priority) | |
|---|---|---|
| Runtime မှာ | Object တစ်ခု ထုတ်ပေးတယ် | ဘာမှ မကျန်ပါ (erased) |
| ရေးပုံ | TaskStatus.Done | "high" |
| သင့်တော်တဲ့နေရာ | Loop ပတ်ဖို့/နာမည်ပေးဖို့ လိုတဲ့ set | ရိုးရိုး ကန့်သတ်ချက် |
- Task ရဲ့ field တွေကို စာရင်းလုပ်
- Value set တွေကို enum/union နဲ့ ပိတ်
- Task တွေကို typed array ထဲ သိမ်း
- Array ပေါ်မှာ function တွေ ရေး
Shape ပြောင်းရင် interface တစ်နေရာတည်းမှာ ပြင်ပါ။ ကျန်တဲ့ code ထဲက မကိုက်တော့တဲ့နေရာတွေကို compiler က ပြပေးပါလိမ့်မယ်။
အခြေခံ ပုံစံ
enum Status { First = "FIRST", Second = "SECOND" }
type Level = "low" | "high";
interface Item {
id: number;
status: Status;
level: Level;
}
const items: Item[] = [];
function addItem(level: Item["level"]): void { /* build + push */ }Closed set တွေကို enum ဒါမှမဟုတ် union နဲ့ သတ်မှတ်၊ interface ထဲမှာ ပေါင်းစပ်ပြီး typed array နဲ့ function တွေက အဲဒီ interface ကို ပြန်ညွှန်းပါတယ်။
Part 1 code: task ထည့်ပြီး ပြကြည့်မယ်
အောက်က file ထဲမှာ data shape နဲ့ function နှစ်ခုပဲ ပါပါတယ်။ addTask() က id ကို အလိုလို တိုးပေးပြီး status ကို Todo နဲ့ စပေးပါတယ်။ listTasks() က task တစ်ခုကို line တစ်ကြောင်းနဲ့ ပြပါတယ်။ Part 2 မှာ ဒီ code ကိုပဲ class အဖြစ် ပြန်စီမှာမို့ နာမည်တွေကို မှတ်ထားပါ။
enum TaskStatus {
Todo = "TODO",
InProgress = "IN_PROGRESS",
Done = "DONE",
}
interface Task {
id: number;
title: string;
status: TaskStatus;
priority: "low" | "medium" | "high";
}
const tasks: Task[] = [];
let nextId = 1;
function addTask(title: string, priority: Task["priority"]): void {
const task: Task = {
id: nextId++,
title,
status: TaskStatus.Todo,
priority,
};
tasks.push(task);
}
function listTasks(): void {
tasks.forEach((t) => {
console.log(`#${t.id} [${t.status}] ${t.title} (${t.priority})`);
});
}
addTask("Learn TypeScript generics", "high");
addTask("Write project README", "low");
listTasks();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- String enum member
Todo = "TODO",Member တစ်ခုစီကို string value ပေးထားလို့ output ထဲမှာ [TODO] လို့ ပေါ်ပါတယ်။ Value မပေးရင် 0 ကနေစတဲ့ number တွေ ဖြစ်သွားပါမယ်။
- Literal union field
priority: "low" | "medium" | "high";Priority က ဒီ string သုံးခုထဲက တစ်ခုပဲ ဖြစ်ရပါမယ်။ "hgh" လို typo ကို compile error အဖြစ် ဖမ်းပါတယ်။
- Indexed access type
function addTask(title: string, priority: Task["priority"]): void {Union ကို ထပ်မရေးဘဲ Task interface ထဲကနေ ယူသုံးပါတယ်။ Interface ပြင်ရင် ဒီ parameter လည်း လိုက်ပြောင်းပါတယ်။
- Auto-incrementing id
id: nextId++,nextId++ က လက်ရှိ value (1) ကို အရင်သုံးပြီးမှ 2 ကို တိုးပါတယ်။ ဒါကြောင့် ပထမ task က #1၊ ဒုတိယက #2 ဖြစ်ပါတယ်။
- One line per task
tasks.forEach((t) => {t ရဲ့ type ကို Task လို့ TypeScript က သိပြီးသားမို့ t.status၊ t.title စတာတွေကို autocomplete နဲ့ ရေးနိုင်ပြီး မရှိတဲ့ field ကို ခေါ်ရင် error ပြပါတယ်။
#1 [TODO] Learn TypeScript generics (high)
#2 [TODO] Write project README (low)ဒီ project က ဘယ်ကိုသွားမလဲ
ဒီ Part 1 code မှာ အားနည်းချက်တစ်ခု ရှိပါတယ်။ tasks နဲ့ nextId က global variable တွေမို့ file ထဲက ဘယ် code မဆို tasks.length = 0 လို့ ရေးပြီး list တစ်ခုလုံးကို ဖျက်ပစ်နိုင်ပါတယ်၊ nextId = 1 လို့ ပြန်ထားပြီး id ထပ်နေအောင်လည်း လုပ်နိုင်ပါတယ်။ List နှစ်ခု (ဥပမာ "Work" နဲ့ "Home") ထားချင်ရင်လည်း မရပါဘူး။
Part 2 မှာ ဒီ state နဲ့ function တွေကို TaskManager class တစ်ခုထဲ private field တွေအဖြစ် ထည့်ပြီး status update နဲ့ filter ကို ထပ်ထည့်ပါမယ်။ Part 3 မှာတော့ storage logic ကို generic Repository<T> အဖြစ် ခွဲထုတ်ပြီး tuple နဲ့ stats ပြန်ပေးတာ၊ error handling တွေ ထည့်ပါမယ်။ Interface Task နဲ့ enum TaskStatus ကတော့ အဆုံးထိ မပြောင်းဘဲ အတူတူပဲ ဆက်သုံးသွားမှာပါ။
Enum က runtime code ထုတ်ပါတယ်
Enum က type တစ်ခုတည်း မဟုတ်ဘဲ JavaScript object ပါ ထုတ်ပေးတဲ့ TypeScript feature အနည်းငယ်ထဲက တစ်ခုပါ။ Type တွေကိုသာ ဖျက်ပစ်တဲ့ tool တွေ (ဥပမာ Node.js ရဲ့ type stripping ဒါမှမဟုတ် erasableSyntaxOnly option) သုံးတဲ့ project တွေမှာ enum ကို ခွင့်မပြုပါဘူး။ အဲဒီလို project မှာဆိုရင် as const object နဲ့ union type ကို အစားထိုးသုံးပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Issue tracker တွေ၊ order system တွေ၊ support ticket app တွေ အားလုံးမှာ "status" field ရှိပြီး value set က ပိတ်ထားပါတယ်။ Team တွေက database column၊ API response နဲ့ frontend dropdown တစ်ခုလုံးမှာ value တူတူကို သုံးနိုင်အောင် ဒီလို enum ဒါမှမဟုတ် union ကို shared type file တစ်ခုထဲမှာ ထားလေ့ရှိပါတယ်။ String value တွေက JSON ထဲမှာ၊ log ထဲမှာ၊ database ထဲမှာ ဖတ်လို့ရလို့ debug လုပ်ရတာ အများကြီး လွယ်ပါတယ်။
သတိထားရမယ့်အချက်က data သိမ်းပြီးသွားရင် string value ("IN_PROGRESS") ကို မပြောင်းသင့်တော့တာပါ။ Database ထဲမှာ ရှိပြီးသား row တွေက value အဟောင်းကို ကိုင်ထားလို့ပါ။ Member နာမည် (InProgress) ကိုတော့ code ထဲမှာ rename လုပ်လို့ ရပါတယ်။ Field အသစ် (ဥပမာ dueDate) ထည့်တဲ့အခါ optional (?) အဖြစ် အရင်ထည့်ရင် data အဟောင်းတွေ ဆက်အလုပ်လုပ်ပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Enum ကို value မပေးဘဲ ထားခဲ့တာ
enum TaskStatus { Todo, InProgress, Done } လို့ ရေးရင် member တွေက 0၊ 1၊ 2 ဖြစ်သွားပြီး log မှာ "#1 [0] ..." လို့ ပေါ်ပါတယ်။ ပိုဆိုးတာက နောက်ပိုင်း Blocked ဆိုတဲ့ member ကို အလယ်မှာ ထည့်လိုက်ရင် Done ရဲ့ number ပြောင်းသွားပြီး သိမ်းထားပြီးသား data တွေ အဓိပ္ပာယ်လွဲသွားပါတယ်။ Member တိုင်းကို string value ပေးပါ။
Priority ကို string လို့ ကျယ်ကျယ် ရေးတာ
priority: string လို့ ရေးလိုက်ရင် addTask("Fix bug", "hgih") က compile ဖြစ်သွားပြီး "high" ကို filter လုပ်တဲ့အခါ ဒီ task ပျောက်နေပါလိမ့်မယ်။ Value set သိရင် literal union ကို သုံးပါ။
Union ကို နေရာတိုင်းမှာ copy ကူးတာ
Function signature တိုင်းမှာ "low" | "medium" | "high" ကို ထပ်ရေးထားရင် value အသစ်ထည့်တဲ့အခါ နေရာတစ်ခု ကျန်ခဲ့တတ်ပါတယ်။ Task["priority"] ဒါမှမဟုတ် type Priority alias တစ်ခုကို ပြန်ညွှန်းပါ။
Mini challenge
Due date နဲ့ status count ထည့်ကြည့်ပါ
Part 1 code ကို ချဲ့ပါ။ Task မှာ optional dueDate ထည့်ပြီး addTask() က due date ကို optional parameter အဖြစ် လက်ခံပါ။ formatTask() နဲ့ countByStatus() ကို ရေးပါ။
- Task interface ထဲမှာ dueDate?: string ကို ထည့်ပါ
- addTask() က ဖန်တီးလိုက်တဲ့ Task ကို return ပြန်ပါ
- formatTask() က due date ရှိရင် "- due ..."၊ မရှိရင် "- no due date" ကို ဆက်ရေးပါ
- countByStatus(status) က status တူတဲ့ task အရေအတွက်ကို ပြန်ပေးပါ
// Reuse TaskStatus and Task from the lesson.
// TODO: add an optional dueDate (string) to the Task interface.
function addTask(
title: string,
priority: Task["priority"],
dueDate?: string,
): Task {
// TODO: create the task with the next id and status Todo, push it, return it
}
function formatTask(task: Task): string {
// TODO: "#1 [TODO] Title (high) - due 2026-12-01"
// or "#2 [TODO] Title (low) - no due date"
}
function countByStatus(status: TaskStatus): number {
// TODO: count the tasks whose status matches
}ရလဒ်: Task တစ်ခုစီက due date ပါ/မပါ ပြတဲ့ line တစ်ကြောင်းစီ ထွက်ပြီး နောက်ဆုံးမှာ Todo: 1 နဲ့ Done: 1 ဆိုတဲ့ count တွေ ပေါ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Optional field ကို ဖတ်တဲ့အခါ type က string | undefined ဖြစ်ပါတယ်။ task.dueDate ? ... : ... နဲ့ စစ်ပါ။ Count အတွက် filter(...).length ကို သုံးနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
enum TaskStatus {
Todo = "TODO",
InProgress = "IN_PROGRESS",
Done = "DONE",
}
interface Task {
id: number;
title: string;
status: TaskStatus;
priority: "low" | "medium" | "high";
dueDate?: string;
}
const tasks: Task[] = [];
let nextId = 1;
function addTask(
title: string,
priority: Task["priority"],
dueDate?: string,
): Task {
const task: Task = { id: nextId++, title, status: TaskStatus.Todo, priority, dueDate };
tasks.push(task);
return task;
}
function formatTask(task: Task): string {
const due = task.dueDate ? `due ${task.dueDate}` : "no due date";
return `#${task.id} [${task.status}] ${task.title} (${task.priority}) - ${due}`;
}
function countByStatus(status: TaskStatus): number {
return tasks.filter((t) => t.status === status).length;
}
addTask("Prepare demo", "high", "2026-12-01");
const readme = addTask("Update README", "low");
readme.status = TaskStatus.Done;
tasks.forEach((t) => console.log(formatTask(t)));
console.log(`Todo: ${countByStatus(TaskStatus.Todo)}`);
console.log(`Done: ${countByStatus(TaskStatus.Done)}`);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Project ကို function ကနေမစဘဲ data shape (interface) ကနေ စပါ
- Loop ပတ်ဖို့ လိုတဲ့ status လို set အတွက် string enum၊ ရိုးရိုး priority အတွက် literal union သုံးပါ
- Task["priority"] လို indexed access type နဲ့ union ကို တစ်နေရာတည်းမှာပဲ ထားပါ
- Global state က Part 2 မှာ class ထဲ ရွှေ့ရမယ့် အားနည်းချက်ပါ
TypeScript Handbook — TypeScript