ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Part 1 မှာ task list က file အလယ်မှာ ပွင့်ထားတဲ့ စာအုပ်တစ်အုပ်လိုပါ။ ဘယ်သူမဆို စာမျက်နှာ ဆုတ်ပစ်လို့ရတယ်၊ နံပါတ်တွေကို ပြန်ရေးလို့ရတယ်။ Part 2 မှာ အဲဒီစာအုပ်ကို counter နောက်မှာ ထားလိုက်ပြီး စာရေးတစ်ယောက် (TaskManager) ကို တာဝန်ပေးပါမယ်။ "Task ထည့်ပေး"၊ "#1 ကို In progress ပြောင်းပေး"၊ "Done ဖြစ်ပြီးသားတွေ ပြပေး" လို့ တောင်းဆိုလို့ရပေမယ့် စာအုပ်ကို ကိုယ်တိုင် ကိုင်လို့ မရတော့ပါဘူး။
Class က data (tasks array နဲ့ nextId) နဲ့ အဲဒီ data ကို ကိုင်တွယ်တဲ့ method တွေကို တစ်နေရာတည်းမှာ စုထားပေးပါတယ်။ private keyword က အပြင်ကနေ တိုက်ရိုက်ထိဖို့ ကြိုးစားရင် compile error ပြပါတယ်။
Problem → Why → Solution
ပြဿနာ: ဘယ်သူမဆို ပြင်လို့ရတဲ့ global state
Part 1 code က program သေးသေးလေး အတွက်တော့ အဆင်ပြေပါတယ်။ ဒါပေမယ့် file ကြီးလာတာနဲ့အမျှ tasks နဲ့ nextId ကို file ထဲက ဘယ် function မဆို ပြင်နိုင်ပါတယ်။ တစ်ယောက်ယောက်က "reset" helper တစ်ခု ထည့်ပြီး nextId ကိုပဲ 1 ပြန်ထားလိုက်ရင် task နှစ်ခုက #1 ဖြစ်သွားပြီး updateStatus(1, ...) က မှားတဲ့ task ကို ပြောင်းမိပါလိမ့်မယ်။
နောက်ပြဿနာတစ်ခုက list တစ်ခုပဲ ရှိနိုင်တာပါ။ "Work" list နဲ့ "Home" list ခွဲထားချင်ရင် variable နဲ့ function အားလုံးကို copy ကူးရပါမယ်။
const tasks: Task[] = [];
let nextId = 1;
// ...200 lines later, a helper someone added:
function resetCounter(): void {
nextId = 1; // ids now repeat: the next task is #1 again
}
// A second "Home" list? Copy tasks, nextId and every function.ဘာကြောင့် ဒါက အန္တရာယ်များတာလဲ
Data ကို ပြင်လို့ရတဲ့ နေရာ များလေ "ဘယ်သူ ဒီ value ကို ပြောင်းလိုက်တာလဲ" ဆိုတာ ရှာရ ခက်လေပါပဲ။ State ကို class ထဲမှာ ပိတ်ထားပြီး method တွေကနေပဲ ပြောင်းခွင့်ပေးရင် rule တွေ (id မထပ်ရ၊ status က enum ထဲကပဲ ဖြစ်ရ) ကို နေရာတစ်ခုတည်းမှာ စောင့်ကြည့်နိုင်ပါတယ်။
Core concept: state နဲ့ behaviour ကို class ထဲ စုပါ
Class တစ်ခုက blueprint ပါ။ new TaskManager() လို့ ခေါ်တိုင်း tasks array နဲ့ nextId ကို ကိုယ်ပိုင်ပိုင်ဆိုင်တဲ့ object အသစ်တစ်ခု ရပါတယ်။ ဒါကြောင့် work နဲ့ home ဆိုတဲ့ manager နှစ်ခုကို ဖန်တီးရင် တစ်ခုရဲ့ id တွေက နောက်တစ်ခုကို မထိခိုက်ပါဘူး။ Method တွေထဲမှာ this.tasks လို့ ရေးပြီး "ဒီ object ရဲ့ tasks" ကို ညွှန်းပါတယ်။
private က TypeScript compiler အတွက် rule တစ်ခုပါ။ Class အပြင်ကနေ manager.tasks လို့ ရေးရင် "Property 'tasks' is private" ဆိုတဲ့ compile error ရပါတယ်။ ဒါပေမယ့် type တွေလိုပဲ private ဆိုတဲ့ စကားလုံးလည်း runtime မှာ ပျောက်သွားပါတယ်။ JavaScript object ထဲမှာ tasks property က ရှိနေဆဲမို့ console.log(manager) ဒါမှမဟုတ် JSON.stringify(manager) နဲ့ မြင်ရပါတယ်။ Runtime မှာပါ တကယ်ဖုံးထားချင်ရင် JavaScript ရဲ့ #tasks (hash private field) ကို သုံးပါ။
Method အသစ်နှစ်ခုက ယခင် lesson တွေက concept တွေကို ပြန်သုံးပါတယ်။ updateStatus() မှာ find() က Task | undefined ပြန်ပေးလို့ if (!task) return false; နဲ့ narrowing လုပ်ပြီးမှ task.status ကို ပြောင်းပါတယ်။ ရှာမတွေ့ရင် false ပြန်ပေးပြီး caller ကို ဆုံးဖြတ်ခွင့်ပေးပါတယ်။ filterByStatus() ကတော့ filter() က ပြန်ပေးတဲ့ array အသစ်ကို return လုပ်ပါတယ်။ Array က အသစ်ဖြစ်ပေမယ့် အထဲက task object တွေကတော့ manager ထဲက object တွေနဲ့ အတူတူပဲ ဖြစ်ပါတယ်။
Method တစ်ခုစီရဲ့ return type က caller ကို ဘာမျှော်လင့်ရမလဲ ပြောပြနေတာကို သတိထားကြည့်ပါ။ addTask() က Task ပြန်ပေးပါတယ်၊ caller က id အသစ်ကို များသောအားဖြင့် လိုလို့ပါ။ updateStatus() က boolean ပြန်ပေးပါတယ်၊ "အောင်မြင်လား" ဆိုတဲ့ မေးခွန်းတစ်ခုပဲ ရှိလို့ပါ။ filterByStatus() က Task[] ပြန်ပေးပါတယ်၊ အလွတ်ဖြစ်နိုင်ပေမယ့် undefined ဘယ်တော့မှ မဖြစ်လို့ caller က forEach() ကို အမြဲခေါ်နိုင်ပါတယ်။ listTasks() ကတော့ print ပဲ လုပ်လို့ void ပါ။ ဒီ type တွေကို စဉ်းစားပြီး ရွေးတာက class design ရဲ့ အစိတ်အပိုင်းပါ။ Part 3 မှာ boolean ကို throw လုပ်တဲ့ error နဲ့ အစားထိုးပြီး trade-off ကို ကြည့်ပါမယ်။
filter() copies the array, not the objects
------------------------------------------
manager.tasks ------> [ ref1 , ref2 ]
| |
v v
{ id: 1 } { id: 2 }
^
|
filterByStatus() ---> [ ref1 ] (new array, same object)| Part 1 (functions) | Part 2 (class) | |
|---|---|---|
| State နေရာ | Global variable | private field |
| List အရေအတွက် | တစ်ခုပဲ | new နဲ့ လိုသလောက် |
| ပြင်ခွင့်ရှိသူ | File ထဲက code အားလုံး | Class ရဲ့ method တွေပဲ |
- new က ကိုယ်ပိုင် state ရတဲ့ object ဆောက်
- private field တွေကို method ကပဲ ထိ
- find() ရလဒ်ကို narrowing နဲ့ စစ်
- filter() က array အသစ် ပြန်ပေး
Caller တွေက method ကနေပဲ တောင်းဆိုရလို့ array ကို နောက်ပိုင်း Map ဒါမှမဟုတ် database နဲ့ အစားထိုးတဲ့အခါ caller code ကို ပြင်စရာ မလိုပါဘူး။
အခြေခံ ပုံစံ
class Manager {
private items: Item[] = [];
add(item: Item): Item { this.items.push(item); return item; }
update(id: number, change: Partial<Item>): boolean {
const found = this.items.find((i) => i.id === id);
if (!found) return false; // narrowed: Item | undefined -> Item
Object.assign(found, change);
return true;
}
}State ကို private field အဖြစ်ထား၊ method တွေကနေပဲ ပြောင်း၊ find() ရလဒ်ကို undefined ဟုတ်/မဟုတ် စစ်ပြီးမှ သုံးပါ။
Part 2 code: TaskManager class
TaskStatus နဲ့ Task က Part 1 အတိုင်းပါပဲ။ ပြောင်းသွားတာက addTask() နဲ့ listTasks() က class method ဖြစ်လာတာ၊ addTask() က ဖန်တီးလိုက်တဲ့ Task ကို return ပြန်တာ၊ updateStatus() နဲ့ filterByStatus() ဆိုတဲ့ method အသစ်နှစ်ခု ပါလာတာပါ။ Demo က မရှိတဲ့ id #99 ကိုပါ စမ်းထားလို့ false ပြန်လာတာကိုလည်း မြင်ရပါမယ်။
enum TaskStatus {
Todo = "TODO",
InProgress = "IN_PROGRESS",
Done = "DONE",
}
interface Task {
id: number;
title: string;
status: TaskStatus;
priority: "low" | "medium" | "high";
}
class TaskManager {
private tasks: Task[] = [];
private nextId = 1;
addTask(title: string, priority: Task["priority"]): Task {
const task: Task = {
id: this.nextId++,
title,
status: TaskStatus.Todo,
priority,
};
this.tasks.push(task);
return task;
}
listTasks(): void {
this.tasks.forEach((t) =>
console.log(`#${t.id} [${t.status}] ${t.title} (${t.priority})`)
);
}
updateStatus(id: number, status: TaskStatus): boolean {
const task = this.tasks.find((t) => t.id === id);
if (!task) return false;
task.status = status;
return true;
}
filterByStatus(status: TaskStatus): Task[] {
return this.tasks.filter((t) => t.status === status);
}
}
const manager = new TaskManager();
manager.addTask("Learn generics", "high");
manager.addTask("Deploy project", "medium");
console.log("update #1:", manager.updateStatus(1, TaskStatus.InProgress));
console.log("update #99:", manager.updateStatus(99, TaskStatus.Done));
console.log("In Progress tasks:");
manager.filterByStatus(TaskStatus.InProgress).forEach((t) =>
console.log(`- ${t.title}`)
);
manager.listTasks();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Private state
private tasks: Task[] = [];Instance တစ်ခုစီက ကိုယ်ပိုင် array ရပါတယ်။ Class အပြင်ကနေ manager.tasks လို့ ရေးရင် compile error ဖြစ်ပါတယ်။
- Return the new task
return task;addTask() က Task ကို ပြန်ပေးလို့ caller က id ကို ချက်ချင်း သိနိုင်ပြီး နောက်ထပ် updateStatus() ခေါ်ရာမှာ သုံးနိုင်ပါတယ်။
- find() may miss
const task = this.tasks.find((t) => t.id === id);find() ရဲ့ return type က Task | undefined ပါ။ ရှာမတွေ့နိုင်တာကို type ကိုယ်တိုင်က သတိပေးနေတာပါ။
- Narrow, then mutate
if (!task) return false;ဒီ line အောက်မှာ task က Task ဖြစ်သွားပါပြီ။ task.status = status က array ထဲက object အစစ်ကို ပြောင်းလို့ listTasks() မှာ [IN_PROGRESS] ပေါ်ပါတယ်။
- A new array
return this.tasks.filter((t) => t.status === status);filter() က မူရင်း array ကို မပြင်ဘဲ ကိုက်ညီတဲ့ object တွေပါတဲ့ array အသစ်ကို ပြန်ပေးပါတယ်။
update #1: true
update #99: false
In Progress tasks:
- Learn generics
#1 [IN_PROGRESS] Learn generics (high)
#2 [TODO] Deploy project (medium)- this.tasks ထဲမှာ id နဲ့ ရှာ
- ရလဒ်က Task | undefined
- undefined ဆိုရင် false ပြန်
- Task ဆိုရင် status ပြောင်းပြီး true ပြန်
id #1 (ရှိတယ်)
true
id #99 (မရှိဘူး)
false
find() က object အစစ်ကို ပေးတယ်
updateStatus() အလုပ်လုပ်တာက find() က copy မဟုတ်ဘဲ array ထဲက object reference ကို ပြန်ပေးလို့ပါ။ ဒါက အသုံးဝင်ပေမယ့် class အပြင်ကိုလည်း ပေါက်ကြားနိုင်ပါတယ်။ filterByStatus() ကနေ ရလာတဲ့ task ကို caller က t.status = TaskStatus.Done လို့ ပြင်ရင် manager ထဲက data ပါ ပြောင်းသွားပြီး updateStatus() ရဲ့ rule တွေကို ကျော်သွားပါတယ်။
ဒီ project အတွက်တော့ ဒါကို လက်ခံထားပါမယ်။ Part 3 မှာ list တစ်ခုလုံးကို ပြန်ပေးတဲ့ method က readonly Task[] ကို return လုပ်ပြီး caller တွေ push() လုပ်လို့ မရအောင် ကာကွယ်ပါမယ်။ Object field တွေကိုပါ ကာကွယ်ချင်ရင် Readonly<Task> ဒါမှမဟုတ် copy ({ ...task }) ကို ပြန်ပေးနိုင်ပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Backend framework တွေမှာ ဒီ pattern ကို "service" class လို့ ခေါ်လေ့ရှိပါတယ်။ OrderService၊ CartService စတဲ့ class တွေက data ကို private ထားပြီး addItem()၊ updateStatus() လို method တွေကနေပဲ ပြောင်းခွင့်ပေးပါတယ်။ Frontend မှာလည်း store တွေက ဒီလိုပဲ အလုပ်လုပ်ပါတယ်။
API တစ်ခုမှာ updateStatus() က false ပြန်လာရင် route handler က HTTP 404 Not Found အဖြစ် ပြောင်းပေးတာမျိုး လုပ်ပါတယ်။ Caller တွေက method ကိုပဲ မှီခိုလို့ array ကနေ Map<number, Task>၊ နောက်ပိုင်း database table အထိ storage ပြောင်းတဲ့အခါ method signature မပြောင်းသရွေ့ ကျန်တဲ့ code ကို ပြင်စရာ မလိုပါဘူး။ Unit test ရေးတာလည်း လွယ်ပါတယ်၊ test တစ်ခုစီက new TaskManager() အသစ်တစ်ခုနဲ့ စလို့ test အချင်းချင်း state မျှမသုံးပါဘူး။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
filter() ရလဒ်ကို ပြန်မသိမ်းတာ
removeTask() ကို this.tasks.filter((t) => t.id !== id); လို့ပဲ ရေးလိုက်ရင် ဘာမှ ဖယ်ရှားမသွားပါဘူး။ filter() က array အသစ်ကို ပြန်ပေးပြီး မူရင်းကို မပြင်လို့ပါ။ this.tasks = this.tasks.filter(...) လို့ ပြန် assign လုပ်ရပါမယ်။ filter() ကို ခေါ်ပြီး ရလဒ်ကို လျစ်လျူရှုတာက TypeScript အရ လုံးဝ မှန်ကန်တဲ့ code ဖြစ်လို့ compiler က သတိမပေးပါဘူး။
private က runtime မှာ ဖုံးပေးမယ်ထင်တာ
private က compile time check ပဲ ဖြစ်ပါတယ်။ JSON.stringify(manager) က {"tasks":[...],"nextId":3} ကို ထုတ်ပေးပါတယ်။ Secret တွေ (token၊ password) ကို runtime မှာပါ ဖုံးချင်ရင် #private field ကို သုံးပါ။
Method ကို ခွဲထုတ်ပြီး this ပျောက်တာ
const add = manager.addTask; add("x", "low"); လို့ ခေါ်ရင် this က undefined ဖြစ်ပြီး this.nextId ကို ဖတ်တဲ့နေရာမှာ TypeError တက်ပါတယ်။ Callback အဖြစ် ပေးချင်ရင် (title) => manager.addTask(title, "low") လို arrow function နဲ့ ထုပ်ပါ။
Best Practices
ကောင်းမွန်တဲ့ အလေ့အကျင့်များ
- Field တွေကို default အနေနဲ့ private ထားပြီး အပြင်က တကယ်လိုမှ method တစ်ခု ဖွင့်ပေးပါ
- Method နာမည်ကို ရည်ရွယ်ချက်အတိုင်း ပေးပါ (updateStatus၊ filterByStatus)၊ setTasks လို generic setter မပေးပါနဲ့
- "မတွေ့ဘူး" ဖြစ်နိုင်တဲ့ method တွေမှာ undefined၊ false ဒါမှမဟုတ် error ထဲက တစ်ခုကို ရွေးပြီး class တစ်ခုလုံးမှာ တသမတ်တည်း သုံးပါ
Mini challenge
removeTask() နဲ့ countByPriority() ထည့်ပါ
TaskManager class ထဲကို method နှစ်ခု ထပ်ထည့်ပါ။ removeTask() က id နဲ့ task ကို ဖယ်ရှားပြီး တကယ် ဖယ်ရှားခဲ့သလား ပြန်ပြောပါ။ countByPriority() က priority တစ်ခုစီမှာ task ဘယ်နှခု ရှိလဲ ပြန်ပေးပါ။
- removeTask(id: number): boolean ကို filter() နဲ့ ရေးပြီး ရလဒ်ကို this.tasks ထဲ ပြန်သိမ်းပါ
- မရှိတဲ့ id ဆိုရင် false ပြန်ပေးပါ
- countByPriority() ရဲ့ return type က Record<Task["priority"], number> ဖြစ်ရပါမယ်
- Task မရှိတဲ့ priority အတွက်လည်း 0 ပါရပါမယ်
// Add these methods inside the TaskManager class from the lesson.
removeTask(id: number): boolean {
// TODO: remember how many tasks there were
// TODO: keep every task except this id, and assign the result back
// TODO: return true only if the length went down
}
countByPriority(): Record<Task["priority"], number> {
// TODO: start from { low: 0, medium: 0, high: 0 }
// TODO: loop over this.tasks and add 1 to counts[t.priority]
}ရလဒ်: removeTask(2) ကို ပထမအကြိမ် true၊ ဒုတိယအကြိမ် false ပြန်ပြီး နောက်ဆုံးမှာ { low: 1, medium: 0, high: 1 } ပေါ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Record<Task["priority"], number> type ပေးထားတဲ့ object မှာ key သုံးခုလုံး မပါရင် compiler က error ပြလို့ 0 သုံးခုနဲ့ စရပါမယ်။ counts[t.priority]++ က t.priority ရဲ့ type ကြောင့် type-safe ဖြစ်ပါတယ်။
အဖြေ ကြည့်ရန်
enum TaskStatus {
Todo = "TODO",
InProgress = "IN_PROGRESS",
Done = "DONE",
}
interface Task {
id: number;
title: string;
status: TaskStatus;
priority: "low" | "medium" | "high";
}
class TaskManager {
private tasks: Task[] = [];
private nextId = 1;
addTask(title: string, priority: Task["priority"]): Task {
const task: Task = { id: this.nextId++, title, status: TaskStatus.Todo, priority };
this.tasks.push(task);
return task;
}
removeTask(id: number): boolean {
const before = this.tasks.length;
this.tasks = this.tasks.filter((t) => t.id !== id);
return this.tasks.length < before;
}
countByPriority(): Record<Task["priority"], number> {
const counts: Record<Task["priority"], number> = { low: 0, medium: 0, high: 0 };
for (const t of this.tasks) counts[t.priority]++;
return counts;
}
}
const home = new TaskManager();
home.addTask("Pay electricity bill", "high");
home.addTask("Fix the shelf", "high");
home.addTask("Water the plants", "low");
console.log(home.removeTask(2));
console.log(home.removeTask(2));
console.log(home.countByPriority());Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Class က state နဲ့ method တွေကို စုထားပြီး new တိုင်း ကိုယ်ပိုင် state ရပါတယ်
- private က compile-time rule ပါ၊ runtime privacy လိုရင် #private ကို သုံးပါ
- find() ရဲ့ Task | undefined ကို narrowing လုပ်ပြီးမှ သုံးပြီး ရှာမတွေ့ရင် false ပြန်ပါ
- filter() က array အသစ်ပြန်ပေးပြီး object တွေကတော့ မူရင်းအတိုင်းပဲ ဖြစ်ပါတယ်
TypeScript Handbook — TypeScript