ရိုးရိုးလေး ရှင်းပြမယ်
TypeScript ကို ရိုးရိုးလေး ပြောရရင်
TypeScript ဆိုတာ JavaScript ကိုပဲ ရေးနေတာပါ၊ ဒါပေမဲ့ value တစ်ခုချင်းက ဘယ်လို အမျိုးအစား (type) ဖြစ်ရမလဲဆိုတာကို မှတ်စုလေးတွေ ထပ်ထည့်ပေးလို့ ရတာပါ။ ဥပမာ `name` က စာသား (string) ဖြစ်ရမယ်၊ `count` က ဂဏန်း (number) ဖြစ်ရမယ် ဆိုပြီး ရေးထားလိုက်ပါတယ်။
အဲဒီ မှတ်စုတွေကို TypeScript compiler က code မ run ခင်မှာ စစ်ပေးပါတယ်။ ဂဏန်းနေရာမှာ စာသား ထည့်မိရင် editor ထဲမှာတင် အနီရောင်မျဉ်းနဲ့ ပြပေးပါတယ်။ စစ်ပြီးသွားရင် မှတ်စုတွေကို ဖျက်ပြီး browser နဲ့ Node.js နားလည်တဲ့ JavaScript သက်သက်ကို ထုတ်ပေးပါတယ်။ ဒါကြောင့် JavaScript အတွက် စာလုံးပေါင်းစစ်တဲ့ spell-checker လို့ မြင်ကြည့်လို့ ရပါတယ်၊ ဒါပေမဲ့ စာလုံးပေါင်းအစား data ရဲ့ ပုံသဏ္ဌာန်ကို စစ်ပေးတာပါ။
Problem → Why → Solution
ပြဿနာ: JavaScript က မှားတဲ့ call ကို တိတ်တိတ်လေး လက်ခံတယ်
ကျောင်းသားတစ်ယောက်ရဲ့ lesson ဘယ်နှခု ကျန်သေးလဲ ဆိုတာကို တွက်ပေးတဲ့ JavaScript function လေးတစ်ခုကို ကြည့်ရအောင်။ ပထမ call မှာ form ကနေ ရလာတဲ့ စာသား `"28"` ကို ထည့်လိုက်တယ်၊ ဒုတိယ call မှာ argument တစ်ခု မေ့သွားတယ်။ JavaScript က error တစ်ခုမှ မပြပါဘူး။
ပထမ call က ကံကောင်းလို့ မှန်တဲ့အဖြေ ထွက်လာတာပါ (`-` operator က စာသားကို ဂဏန်းအဖြစ် ပြောင်းပေးလို့ပါ)။ ဒုတိယ call ကတော့ `NaN` ထုတ်ပေးပြီး အဲဒီ `NaN` က screen ပေါ်ရောက်တဲ့အထိ ဆက်စီးသွားပါတယ်။ User တစ်ယောက်က 'NaN lessons left' ဆိုတာကို မြင်ပြီးမှ bug ကို သိရပါတယ်။
// Plain JavaScript: nothing describes what total and done should be
function lessonsLeft(total, done) {
return total - done;
}
console.log(lessonsLeft("28", 6)); // 22 - works only by luck
console.log(lessonsLeft(28)); // NaN - forgot an argument, no warningဘာကြောင့် ဒါက ပြဿနာလဲ
JavaScript က function တစ်ခုက ဘယ်လို value မျိုး လိုချင်လဲဆိုတာကို ဘယ်နေရာမှာမှ မှတ်မထားပါဘူး၊ ဒါကြောင့် bug ကို code run တဲ့အချိန်၊ အဲဒီ line ကို တကယ် ရောက်တဲ့အချိန်မှပဲ တွေ့ရပါတယ်။ TypeScript မှာတော့ `total: number` လို့ ရေးလိုက်တာနဲ့ မှားတဲ့ call နှစ်ခုလုံးကို ရေးနေတုန်းမှာပဲ compiler က ပြပေးပါတယ်။
အဓိက သဘောတရား: type ဆိုတာ compile time မှာ စစ်တဲ့ ကတိ
TypeScript ကို Microsoft က ဖန်တီးပြီး open source အနေနဲ့ ထုတ်ထားတာပါ။ သူ့ရဲ့ အလုပ်က တစ်ခုတည်းပါ: သင့် JavaScript ထဲက value တွေ၊ function တွေ ဘယ်လို ပုံသဏ္ဌာန်ရှိရမလဲဆိုတာကို ဖော်ပြခွင့်ပေးပြီး အဲဒီ ဖော်ပြချက်နဲ့ code တကယ်လုပ်တာ ကိုက်မကိုက် စစ်ပေးတာပါ။ Annotation တစ်ခုစီက 'ဒီ variable ထဲမှာ number ပဲ ရှိရမယ်' ဆိုတဲ့ ကတိ (contract) တစ်ခုပါ။
အရေးကြီးတာက စစ်တဲ့အချိန်ပါ။ Type checking က compile time မှာ ဖြစ်ပါတယ် — ဆိုလိုတာက code မ run ခင်၊ editor ထဲမှာ ရေးနေတုန်း ဒါမှမဟုတ် `tsc` command ကို run တဲ့အချိန်မှာပါ။ Program တကယ် run နေတဲ့ runtime မှာတော့ type တွေ မရှိတော့ပါဘူး၊ compiler က ဖျက်ပစ်ပြီးသားမို့ပါ။
Annotation ရေးပါ
`let count: number = 3;` လိုမျိုး name နောက်မှာ colon နဲ့ type ကို ရေးပါတယ်။ Function parameter တွေနဲ့ return value မှာလည်း အလားတူ ရေးလို့ရပါတယ်။
Compiler က စစ်ပါတယ်
Value တစ်ခုခုက ကတိနဲ့ မကိုက်ရင် file နာမည်၊ line နံပါတ်နဲ့ 'Argument of type string is not assignable to parameter of type number' လိုမျိုး message တစ်ခု ပြပေးပါတယ်။
Type တွေ ဖျက်ပြီး JavaScript ထုတ်ပါတယ်
Output ထဲမှာ `: number` တွေ မပါတော့ပါဘူး။ Browser ဒါမှမဟုတ် Node.js က ပုံမှန် JavaScript တစ်ခုလိုပဲ run ပါတယ်။
ဒီ အဆင့်သုံးဆင့်က course တစ်ခုလုံးရဲ့ အခြေခံပါ။ နောက်ပိုင်း lesson တွေမှာ ပိုရှုပ်တဲ့ type တွေ (union, interface, generic) ကို တွေ့ရမှာပေမဲ့ အားလုံးက ဒီ 'ကတိရေး၊ compiler စစ်၊ JavaScript run' ဆိုတဲ့ လမ်းကြောင်းအတိုင်းပဲ အလုပ်လုပ်ပါတယ်။
TypeScript က တမင် မလုပ်တဲ့ အရာနှစ်ခုကိုလည်း ပထမနေ့ကတည်းက သိထားသင့်ပါတယ်။ JavaScript ရဲ့ အလုပ်လုပ်ပုံကို မပြောင်းပါဘူး: runtime မှာ `"2" + 1` က `"21"` ပဲ ဖြစ်နေဆဲပါ။ နောက်တစ်ခုက အရာအားလုံးကို annotation ရေးခိုင်းတာ မဟုတ်ပါဘူး၊ assign လုပ်တဲ့ value ကနေ type အများစုကို ကိုယ်တိုင် infer လုပ်ပေးလို့ပါ။
- JavaScript + type မှတ်စုများ
- tsc က ကတိတွေကို စစ်တယ်
- Type တွေ ဖျက်ပြီး JS ထုတ်တယ်
- Browser / Node.js က run တယ်
Type တွေက run မတိုင်ခင်မှာပဲ ရှိပါတယ်။ Runtime မှာ ဝင်လာတဲ့ data (API, form) ကို type က အလိုအလျောက် မစစ်ပေးပါဘူး။
အခြေခံ annotation ပုံစံ
let variableName: Type = value;
function functionName(param: ParamType): ReturnType {
return result;
}Colon (`:`) နောက်က အရာတိုင်းက type ပါ။ Variable အတွက်တစ်ခု၊ parameter တစ်ခုစီအတွက်တစ်ခု၊ parenthesis နောက်မှာ return type တစ်ခု ရေးပါတယ်။ ဒီ annotation တွေကို ဖျက်လိုက်ရင် ကျန်တာက JavaScript အတိအကျပါ။
ပထမဆုံး typed program လေး
အောက်က program မှာ parameter နှစ်ခုနဲ့ return type တစ်ခုပါတဲ့ function တစ်ခု ရှိပါတယ်။ ကျောင်းသားကို နှုတ်ဆက်ပြီး lesson ဘယ်နှခု ကျန်သေးလဲ ပြောပေးပါတယ်။ နောက်ဆုံး comment ထဲက line ကို uncomment လုပ်ကြည့်ရင် compiler က ဘာကြောင့် ငြင်းလဲဆိုတာ မြင်ရပါလိမ့်မယ်။
function welcome(name: string, lessonsLeft: number): string {
return `Hi ${name}, ${lessonsLeft} lessons to go!`;
}
const learner: string = "Aye Aye";
const total: number = 28;
const done: number = 1;
console.log(welcome(learner, total - done));
console.log(welcome("Ko Ko", total));
// welcome(total, learner);
// Error: Argument of type 'number' is not assignable to parameter of type 'string'.Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Typed parameters and return type
function welcome(name: string, lessonsLeft: number): string {`name` က string ဖြစ်ရမယ်၊ `lessonsLeft` က number ဖြစ်ရမယ်၊ function က string တစ်ခု ပြန်ပေးရမယ် ဆိုတဲ့ ကတိသုံးခုကို line တစ်ကြောင်းတည်းမှာ ရေးထားပါတယ်။
- Annotated variables
const total: number = 28;Variable ကိုလည်း annotation ပေးလို့ရပါတယ်။ ဒီလောက်ရိုးရှင်းတဲ့ value မှာ TypeScript က type ကို ကိုယ်တိုင် infer လုပ်နိုင်ပါတယ် (Basic Types lesson မှာ ပြန်တွေ့ပါမယ်)၊ ဒီမှာတော့ ပုံစံကို မြင်သာအောင် ရေးပြထားတာပါ။
- A call that matches the contract
console.log(welcome(learner, total - done));`total - done` ရဲ့ ရလဒ်က number ဖြစ်လို့ ဒုတိယ parameter နဲ့ ကိုက်ပါတယ်။ Compiler က လက်ခံပြီး `Hi Aye Aye, 27 lessons to go!` ကို print ထုတ်ပါတယ်။
- A call the compiler rejects
// welcome(total, learner);Argument တွေ နေရာလွဲသွားတဲ့ call ပါ။ JavaScript မှာဆိုရင် 'Hi 28, Aye Aye lessons to go!' လို့ ထုတ်ပြီး အလုပ်လုပ်သလို ဖြစ်နေမှာပါ။ TypeScript ကတော့ run မတိုင်ခင်မှာပဲ error ပြပါတယ်။
Hi Aye Aye, 27 lessons to go!
Hi Ko Ko, 28 lessons to go!- welcome.ts ကို ရေးတယ်
- Compiler က call တိုင်းကို စစ်တယ်
- Type တွေ ဖျက်ပြီး welcome.js ထုတ်တယ်
- Node.js က run ပြီး print ထုတ်တယ်
Type တွေ ကိုက်တယ်
JavaScript is emitted and the greeting is printed
Type တွေ မကိုက်ဘူး
Error with file name, line and the expected type
ဒီ course ကို ဘယ်လို ခွဲထားလဲ
Course ကို အပိုင်းငါးပိုင်း ခွဲထားပြီး အပိုင်းတစ်ခုချင်းက အရင်အပိုင်းပေါ်မှာ အခြေခံပါတယ်။ အစကနေ အစီအစဉ်အတိုင်း သွားဖို့ အကြံပြုပါတယ်။ Advanced အပိုင်းက generic တွေ၊ conditional type တွေဟာ Basic နဲ့ Intermediate မှာ သင်တဲ့ အရာတွေကို ပေါင်းစပ်ထားတာမို့ပါ။
| အပိုင်း | သင်ယူရမယ့်အရာ |
|---|---|
| Basic | TypeScript ဆိုတာ ဘာလဲ၊ setup, strict tsconfig, string / number / boolean လို basic type များ |
| Intermediate | unknown နဲ့ never, function, object, array, tuple, literal type, enum, ES module |
| Advanced | Union, narrowing, interface, class, generic, utility နဲ့ conditional type, runtime validation |
| Projects | Task Manager app လေးကို အပိုင်းသုံးပိုင်းခွဲပြီး အစကနေ ဆောက်ကြည့်တာ |
| Exercises | Basic ကနေ Advanced အထိ ပြန်လေ့ကျင့်ဖို့ practice set နှစ်ခု |
Lesson တိုင်းက အစီအစဉ်တူပါတယ်: ရိုးရိုးရှင်းပြချက်၊ ပြဿနာတစ်ခု၊ အဓိက သဘောတရား၊ ယေဘုယျ syntax ပုံစံ၊ line တစ်ကြောင်းချင်း ရှင်းပြထားတဲ့ ဥပမာ၊ အမှားများ၊ mini challenge နဲ့ quiz သုံးခုပါ။ ဥပမာတိုင်းကို ကိုယ်တိုင် run ကြည့်ပြီး output ကို မ run ခင် ကြိုမှန်းကြည့်ပါ။ JavaScript ရဲ့ variable, function, array, object တွေကို သိထားရင် စလို့ရပါပြီ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
ယနေ့ ကြီးမားတဲ့ web project အများစုမှာ TypeScript ကို တွေ့ရပါတယ်။ React, Vue, Angular, Next.js လို framework တွေက TypeScript ကို ပထမတန်းစား ထောက်ပံ့ပြီး Node.js backend တွေ၊ CLI tool တွေ၊ VS Code လို desktop app တွေကိုလည်း TypeScript နဲ့ ရေးထားပါတယ်။ npm package အများအပြားက type definition ပါပြီးသားမို့ library ကို import လုပ်တာနဲ့ editor က function တိုင်းရဲ့ parameter တွေကို ချက်ချင်း ပြပေးပါတယ်။
အဖွဲ့လိုက် အလုပ်လုပ်တဲ့အခါ type တွေက documentation အလုပ်ပါ လုပ်ပေးပါတယ်။ တခြားသူ ရေးထားတဲ့ function တစ်ခုကို ခေါ်ချင်ရင် source code ကို ဖွင့်ဖတ်စရာ မလိုဘဲ signature ကို ကြည့်ရုံနဲ့ ဘာထည့်ရမလဲ သိပါတယ်။ Field တစ်ခုကို နာမည်ပြောင်းလိုက်ရင် compiler က အဲဒီ field ကို သုံးထားတဲ့ နေရာတိုင်းကို စာရင်းပြပေးတာမို့ codebase ကြီးတစ်ခုကို refactor လုပ်ရတာ ကြောက်စရာ မဟုတ်တော့ဘဲ ပုံမှန်အလုပ်တစ်ခု ဖြစ်သွားပါတယ်။
အမှားများ
အစပိုင်းမှာ မကြာခဏ ဖြစ်တတ်တဲ့ အမှားများ
Type တွေက runtime မှာ data ကို စစ်ပေးတယ်လို့ ထင်တာ
Type တွေက compile ပြီးတာနဲ့ ပျောက်သွားပါတယ်။ API တစ်ခုက `{ "age": "thirty" }` ပြန်ပို့လာရင် `age: number` လို့ ရေးထားပေမဲ့ program က ဘာမှ သတိမပေးဘဲ string ကို လက်ခံသွားပါလိမ့်မယ်။ အပြင်ကလာတဲ့ data ကို runtime မှာ ကိုယ်တိုင် စစ်ရပါတယ်။ ဒါကို Advanced အပိုင်းမှာ zod နဲ့ လေ့လာပါမယ်။
Error ကို `any` နဲ့ ပိတ်ပစ်တာ
အနီမျဉ်းတွေ မြင်ရတာ စိတ်ညစ်စရာ ကောင်းပေမဲ့ `any` ထည့်ပြီး ဖျောက်လိုက်တာက compiler ကို မျက်စိမှိတ်ခိုင်းလိုက်တာပါ။ Error message ကို ဖတ်ပါ: ဘယ် type ကို မျှော်လင့်ထားလဲ၊ ဘာ type ရောက်လာလဲ ဆိုတာ ပြောထားပါတယ်။ အများအားဖြင့် ပြင်ရမှာက annotation မဟုတ်ဘဲ code ထဲက bug အစစ်ပါ။
ဥပမာတွေကို ဖတ်ရုံပဲ ဖတ်တာ
Type error တွေကို ကိုယ်တိုင် မြင်ဖူးမှ မှတ်မိပါတယ်။ ဥပမာတိုင်းမှာ argument တစ်ခုကို တမင် မှားထည့်ကြည့်ပြီး message ကို ဖတ်ကြည့်ပါ။
Mini challenge
Mini challenge: progress message
ပြီးသွားတဲ့ lesson အရေအတွက်နဲ့ စုစုပေါင်း lesson အရေအတွက်ကို လက်ခံပြီး `6/28 lessons done (21%)` လိုမျိုး string တစ်ခု ပြန်ပေးတဲ့ typed function `progressMessage` ကို ရေးပါ။
- Parameter နှစ်ခုလုံးနဲ့ return value ကို annotation ပေးပါ
- ရာခိုင်နှုန်းကို `Math.round` နဲ့ ကိန်းပြည့် လုပ်ပါ
- `total` က 0 ဖြစ်ရင် `0%` ပြပါ (0 နဲ့ မစားပါနဲ့)
- Run မကြည့်ခင် `progressMessage("6", 28)` က ဘာ error ပေးမလဲ ကြိုမှန်းကြည့်ပါ
// TODO: add number types to done and total, and a string return type
function progressMessage(done, total) {
// TODO: work out the percentage (guard against total === 0)
// TODO: return "<done>/<total> lessons done (<percent>%)"
}
// TODO: print the message for 6 of 28 and for 0 of 0ရလဒ်: `6/28 lessons done (21%)` နဲ့ `0/0 lessons done (0%)` ကို print ထုတ်ပါမယ်။ `progressMessage("6", 28)` ကတော့ 'Argument of type string is not assignable to parameter of type number' error ပေးပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
`done / total * 100` ကို `Math.round` ထဲ ထည့်ပါ။ `total === 0` ကို အရင် စစ်ပြီး 0 ကို ပြန်ပေးပါ။
အဖြေ ကြည့်ရန်
function progressMessage(done: number, total: number): string {
const percent = total === 0 ? 0 : Math.round((done / total) * 100);
return `${done}/${total} lessons done (${percent}%)`;
}
console.log(progressMessage(6, 28));
console.log(progressMessage(0, 0));
// progressMessage("6", 28); // Error: string is not assignable to numberQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- TypeScript = JavaScript + type annotation တွေ၊ compiler က run မတိုင်ခင် စစ်ပေးပါတယ်
- Annotation ပုံစံက `name: Type` ပါ၊ variable, parameter, return value တွေမှာ သုံးပါတယ်
- Compile ပြီးရင် type တွေ ဖျက်သွားလို့ runtime data ကို ကိုယ်တိုင် စစ်ရပါတယ်
- Course ကို Basic → Intermediate → Advanced → Projects → Exercises အစီအစဉ်အတိုင်း သွားပါ
Announcing TypeScript 7.0 — TypeScript