ရိုးရိုးလေး ရှင်းပြမယ်
Basic type တွေကို ရိုးရိုးလေး ပြောရရင်
TypeScript မှာ variable တစ်ခု ကြေညာတဲ့အခါ နာမည်နောက်မှာ colon နဲ့ type ကို `let age: number = 30;` လို ရေးလို့ရပါတယ်။ အသုံးအများဆုံး type သုံးခုက စာသားအတွက် `string` ("Hello")၊ ဂဏန်းအတွက် `number` (100, 3.14)၊ ဟုတ်/မဟုတ် အတွက် `boolean` (`true` ဒါမှမဟုတ် `false`) ပါ။
Type ကို ရေးထားပြီးရင် အဲဒီ variable ထဲကို type မတူတဲ့ value ထည့်ခွင့် မရှိတော့ပါဘူး။ ဒါပေမဲ့ အမြဲရေးစရာ မလိုပါဘူး: `let age = 30;` လို့ ရေးရင် TypeScript က `30` ကို ကြည့်ပြီး `number` လို့ ကိုယ်တိုင် သိပါတယ်။ ထူးခြားတဲ့ type တစ်ခုက `any` ပါ၊ ဘာမဆို ထည့်ခွင့်ပေးပြီး type checking ကို ပိတ်ပစ်လိုက်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: `any` က bug ကို runtime အထိ ပို့ပေးတယ်
Config file ကနေ retry အရေအတွက်ကို ဖတ်တယ် ဆိုပါစို့။ File ထဲမှာ `"3"` လို့ string အနေနဲ့ သိမ်းထားပြီး variable ကို `any` လို့ type ပေးထားပါတယ်။ `retries + 1` က `4` မဟုတ်ဘဲ `"31"` ဖြစ်သွားပါတယ်။
`any` ကြောင့် compiler က `attempts` ကိုလည်း `any` လို့ပဲ ယူဆပြီး `attempts.toFixed(0)` ကို ခွင့်ပြုပါတယ်။ Run လိုက်တဲ့အခါမှ string မှာ `toFixed` မရှိလို့ TypeError နဲ့ crash ပါတယ်။ `any` တစ်ခုက သူ့ကို ထိတဲ့ value တိုင်းဆီ ကူးစက်သွားပါတယ်။
let retries: any = "3"; // read from a config file
const attempts = retries + 1; // "31" - attempts is any too
console.log(attempts.toFixed(0)); // TypeError: attempts.toFixed is not a function`number` လို့ ရေးထားရင် ဘာဖြစ်မလဲ
`let retries: number = "3";` လို့ ရေးထားရင် compiler က 'Type string is not assignable to type number' ဆိုပြီး ချက်ချင်း ပြပါတယ်။ String ကို `Number(...)` နဲ့ ပြောင်းဖို့ သတိပေးတဲ့ နေရာက crash ဖြစ်တဲ့ line မဟုတ်ဘဲ data ဝင်လာတဲ့ line ဖြစ်သွားပါတယ်။ `any` ကတော့ ဒီ သတိပေးချက်ကို ဖျက်ပစ်လိုက်တာပါ။
အဓိက သဘောတရား: primitive type, inference နဲ့ any
JavaScript ရဲ့ အခြေခံ value (primitive) အမျိုးအစားတစ်ခုစီအတွက် TypeScript မှာ စာလုံးအသေးနဲ့ ရေးတဲ့ type တစ်ခုစီ ရှိပါတယ်။ ဒီ lesson ရဲ့ အဓိက သုံးခုအပြင် ကျန်တာတွေကိုလည်း မှတ်မိအောင် ဇယားမှာ စုထားပါတယ်။ `string`, `number`, `boolean` ကို အမြဲလိုလို သုံးရမှာဖြစ်ပြီး `bigint` နဲ့ `symbol` (ဇယားမှာ မပါ) ကတော့ နေ့စဉ် application code မှာ ရှားပါတယ်။
| Type | Value ဥပမာ | မှတ်ချက် |
|---|---|---|
| string | "Hello", 'Hi', `Hi ${name}` | Quote သုံးမျိုးစလုံး string ပါ |
| number | 100, 3.14, -7, NaN | ကိန်းပြည့်နဲ့ ဒသမ ခွဲမထားပါ |
| boolean | true, false | "true" (string) နဲ့ မတူပါ |
| bigint | 10n | number ထက် ကြီးတဲ့ ကိန်းပြည့်တွေအတွက် |
| null / undefined | null, undefined | Value မရှိကြောင်း ပြတာ |
`number` မှာ ကိန်းပြည့် type သီးသန့် မရှိပါဘူး။ `5`၊ `5.0`၊ `5.00` က value တစ်ခုတည်းဖြစ်ပြီး print ထုတ်ရင် `5` ပဲ ထွက်ပါတယ်။ ဒသမနေရာ အတိအကျ ပြချင်ရင် `toFixed(1)` လို method နဲ့ string အဖြစ် format လုပ်ရပါတယ်။ ထိုနည်းတူ `NaN` ('not a number') ကိုယ်တိုင်ကလည်း type `number` ဖြစ်လို့ `Number("abc")` လို ပြောင်းလဲမှု မအောင်မြင်တာကိုတောင် compiler က လက်ခံပါတယ်၊ `Number.isNaN` နဲ့ ကိုယ်တိုင် စစ်ရပါတယ်။
Inference ကြောင့် annotation အများစုကို ချန်ထားလို့ ရပါတယ်။ `let title = "Basic Types"` ဆိုရင် `title` က `string` ဖြစ်ပြီး နောက်ပိုင်း `title = 42` က error ဖြစ်ပါတယ်။ Value မပါဘဲ ကြေညာတဲ့ variable တွေ၊ function parameter တွေမှာတော့ annotation ရေးပေးပါ။
`any` ကတော့ 'ဒီ variable ကို မစစ်ပါနဲ့' လို့ ပြောတာပါ။ ဘာ value မဆို ထည့်လို့ရသလို ဘာ method မဆို ခေါ်လို့ရပြီး ရလဒ်တွေကလည်း `any` ဖြစ်သွားပါတယ်။ JavaScript code အဟောင်းကို ပြောင်းနေတုန်း ယာယီ သုံးလို့ရပေမဲ့ code အသစ်မှာ မသုံးသင့်ပါဘူး။ Value ရဲ့ type ကို တကယ် မသိတဲ့အခါ ပိုလုံခြုံတဲ့ `unknown` ကို နောက် lesson မှာ လေ့လာပါမယ်။
- `: type` ရေးထားရင် အဲဒါကို သုံး
- မရေးရင် initial value ကနေ infer
- Value လည်းမရှိရင် annotation ရေး
- `any` = checking ပိတ်ထားတယ်
Type တစ်ခု သတ်မှတ်ပြီးရင် variable ရဲ့ သက်တမ်းတစ်လျှောက် ပြောင်းမသွားပါဘူး။
Basic type ကြေညာပုံ
let textName: string = "text";
let numberName: number = 0;
let flagName: boolean = false;
let inferredName = "value"; // type comes from the value
let laterName: number; // no value yet: annotate itType နာမည်တွေကို စာလုံးအသေးနဲ့ ရေးပါ (`string`၊ `String` မဟုတ်ပါ)။ Initial value ရှိရင် annotation ကို ချန်ထားပြီး TypeScript ကို infer လုပ်ခိုင်းလို့ရပါတယ်။
Basic type တွေကို လက်တွေ့ သုံးကြည့်ခြင်း
ဒီ example မှာ basic type သုံးခု၊ inference နဲ့ `any` ကို တစ်နေရာတည်းမှာ ပြထားပါတယ်။ Version နံပါတ်ကို သေချာကြည့်ပါ: code က `5.0` ကို `number` အဖြစ် သိမ်းထားပြီး JavaScript က အဲဒါကို `5` လို့ print ထုတ်ပါတယ်။ ဒါကြောင့် output မှာ `toFixed(1)` နဲ့ format မလုပ်ခင်နဲ့ လုပ်ပြီးနောက် ရလဒ်နှစ်မျိုးလုံးကို ပြထားပါတယ်။
let framework: string = "TypeScript";
let version: number = 5.0; // 5.0 and 5 are the same number
let isAwesome: boolean = true;
let lessons = 28; // no annotation: TypeScript infers number
// 'any' switches type checking off for this variable
let anything: any = 4;
anything = "Now I'm a string";
console.log(`Learning ${framework} v${version}`);
console.log(`Learning ${framework} v${version.toFixed(1)}`);
console.log(typeof lessons, typeof isAwesome, typeof anything);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Three annotated primitives
let framework: string = "TypeScript";`framework` ထဲကို string ပဲ ထည့်ခွင့်ရှိပါတယ်။ `version` နဲ့ `isAwesome` ကလည်း အလားတူ `number` နဲ့ `boolean` ကတိတွေပါ။
- Inference
let lessons = 28; // no annotation: TypeScript infers numberAnnotation မပါပေမဲ့ `lessons` က `number` ပါ။ နောက်ပိုင်း `lessons = "many"` လို့ ရေးရင် compile error ဖြစ်ပါတယ်။
- any accepts anything
anything = "Now I'm a string";`anything` က number နဲ့ စပေမဲ့ string ကို ပြန်ထည့်လို့ရပါတယ်၊ compiler က ဘာမှ မစစ်တော့လို့ပါ။ နောက်ဆုံး line မှာ `typeof anything` က `string` ဖြစ်နေတာကို တွေ့ရပါတယ်။
- 5.0 prints as 5
console.log(`Learning ${framework} v${version}`);`version` ထဲမှာ `5` ဆိုတဲ့ number တစ်ခုပဲ ရှိလို့ `Learning TypeScript v5` ထွက်ပါတယ်။ Source code မှာ `5.0` လို့ ရေးထားတာက value ကို မပြောင်းပါဘူး။
- Formatting decimals
console.log(`Learning ${framework} v${version.toFixed(1)}`);`toFixed(1)` က ဒသမ တစ်နေရာနဲ့ string `"5.0"` ကို ပြန်ပေးလို့ `Learning TypeScript v5.0` ထွက်ပါတယ်။ `version` က `number` ဖြစ်တာကို editor က သိလို့ `toFixed` ကို autocomplete ပေးပါတယ်။
Learning TypeScript v5
Learning TypeScript v5.0
number boolean string`let` နဲ့ `const` မှာ inference ကွာပုံ
`let mode = "dark";` လို့ ရေးရင် `mode` ရဲ့ type က `string` ပါ၊ `let` variable ကို နောက်ပိုင်း တခြား string နဲ့ ပြောင်းလို့ရလို့ပါ။ `const mode = "dark";` လို့ ရေးရင်တော့ value ဘယ်တော့မှ မပြောင်းနိုင်တာမို့ TypeScript က type ကို `"dark"` ဆိုတဲ့ value တစ်ခုတည်းအဖြစ် ပိုတိတိကျကျ မှတ်ပါတယ်။ ဒါကို literal type လို့ ခေါ်ပြီး Literal Types lesson မှာ အသေးစိတ် လေ့လာပါမယ်။
Editor ထဲမှာ variable နာမည်ပေါ် mouse တင်ကြည့်ရင် TypeScript infer လုပ်ထားတဲ့ type ကို မြင်ရပါတယ်။ `let` နဲ့ `const` နှစ်မျိုးလုံး ရေးပြီး ကွာခြားချက်ကို ကိုယ်တိုင် စမ်းကြည့်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ basic type များ
လက်တွေ့ code ထဲက data အများစုက နောက်ဆုံးမှာ basic type တွေနဲ့ပဲ ဖွဲ့စည်းထားပါတယ်: user ရဲ့ email က `string`၊ ဈေးနှုန်းနဲ့ အရေအတွက်က `number`၊ `isAdmin`, `emailVerified` လို flag တွေက `boolean` ပါ။ Interface နဲ့ object type တွေကို နောက် lesson တွေမှာ လေ့လာတဲ့အခါ အဲဒီ field တစ်ခုစီရဲ့ type က ဒီ lesson က primitive တွေပဲ ဖြစ်ပါတယ်။
ငွေကြေးကို `number` နဲ့ သိမ်းတဲ့အခါ `0.1 + 0.2` က `0.30000000000000004` ဖြစ်တဲ့ floating-point ပြဿနာကို ရှောင်ဖို့ project အများစုက ပြားကို ကိန်းပြည့်အဖြစ် (ဥပမာ 350 cents) သိမ်းပြီး ပြတဲ့အခါမှ `toFixed(2)` နဲ့ format လုပ်ပါတယ်။ ESLint ရဲ့ `no-explicit-any` rule ကိုလည်း ဖွင့်ထားလေ့ရှိပါတယ်၊ ဒါဆို `any` အသစ်တစ်ခု ထည့်တိုင်း merge မလုပ်ခင် code review မှာ ဘာကြောင့်လဲ ရှင်းပြရပါတယ်။
အမှားများ
Basic type တွေနဲ့ ပတ်သက်တဲ့ အမှားများ
`String`, `Number` လို စာလုံးကြီး type တွေ သုံးတာ
`String`, `Number`, `Boolean` က JavaScript ရဲ့ wrapper object တွေကို ဆိုလိုပြီး primitive type တွေ မဟုတ်ပါဘူး။ `let name: String` က အလုပ်လုပ်သလို ဖြစ်နေပေမဲ့ TypeScript docs ကိုယ်တိုင်က မသုံးဖို့ အကြံပြုထားပါတယ်။ အမြဲ စာလုံးအသေး `string`, `number`, `boolean` ကို သုံးပါ။
`5.0` က ဒသမကိန်းအဖြစ် print ထွက်မယ်လို့ မျှော်လင့်တာ
Code ထဲမှာ `5.0` လို့ ရေးထားလို့ output မှာ `v5.0` ထွက်မယ်လို့ မျှော်လင့်မိဖို့ လွယ်ပေမဲ့ တကယ် run ရင် `v5` ထွက်ပါတယ်။ `number` က ဒသမ နေရာ ဘယ်နှခု ရေးခဲ့လဲ ဆိုတာ မမှတ်ထားပါဘူး။ Version နံပါတ်လို text အဖြစ် ပြရမယ့် value ဆိုရင် `string` ("5.0") အဖြစ် သိမ်းပါ၊ ဒါမှမဟုတ် ပြတဲ့အခါ `toFixed` နဲ့ format လုပ်ပါ။
Error ပျောက်အောင် `any` ထည့်တာ
`any` က error ကို ပြင်တာ မဟုတ်ဘဲ compiler ကို မျက်စိမှိတ်ခိုင်းတာပါ။ Problem ထဲကလို bug က runtime အထိ ရောက်သွားပါလိမ့်မယ်။ Value ကို မှန်တဲ့ type အဖြစ် ပြောင်းပါ (`Number("3")`)၊ ဒါမှမဟုတ် type မသိရင် `unknown` ကို သုံးပါ။
Best Practices
Basic type အတွက် အလေ့အထကောင်းများ
- Value ရှိတဲ့ local variable တွေမှာ annotation မရေးဘဲ inference ကို အားကိုးပါ
- အပြင်ကလာတဲ့ string ကို အသုံးမပြုခင် `Number()` နဲ့ ပြောင်းပြီး `Number.isNaN` နဲ့ စစ်ပါ
- Code အသစ်မှာ `any` အစား မှန်ကန်တဲ့ type ဒါမှမဟုတ် `unknown` ကို ရွေးပါ
Mini challenge
Mini challenge: product label
ပစ္စည်းတစ်ခုရဲ့ နာမည်၊ ဈေးနှုန်းနဲ့ stock ရှိမရှိကို basic type တွေနဲ့ ကြေညာပြီး `Notebook - $3.50 - in stock` လိုမျိုး label တစ်ခု ထုတ်ပေးတဲ့ function ရေးပါ။
- `productLabel(name: string, price: number, inStock: boolean): string` ကို ရေးပါ
- ဈေးနှုန်းကို `3.5` ဖြစ်ပါစေ ဒသမ နှစ်နေရာ (`3.50`) နဲ့ ပြပါ
- `inStock` က false ဆိုရင် `sold out` လို့ ပြပါ
- `any` ကို မသုံးရပါ
// TODO: declare a name (string), a price (number) and inStock (boolean)
function productLabel(name: string, price: number, inStock: boolean): string {
// TODO: format the price with two decimals
// TODO: pick "in stock" or "sold out"
// TODO: return "<name> - $<price> - <status>"
}
// TODO: print a label for your product and for one that is sold outရလဒ်: `Notebook - $3.50 - in stock` နဲ့ `Pen - $1.25 - sold out` ကို print ထုတ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
`price.toFixed(2)` နဲ့ ternary operator `inStock ? "in stock" : "sold out"` ကို ပေါင်းသုံးပါ။
အဖြေ ကြည့်ရန်
const productName: string = "Notebook";
const productPrice: number = 3.5;
const available: boolean = true;
function productLabel(name: string, price: number, inStock: boolean): string {
const status = inStock ? "in stock" : "sold out";
return `${name} - $${price.toFixed(2)} - ${status}`;
}
console.log(productLabel(productName, productPrice, available));
console.log(productLabel("Pen", 1.25, false));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Basic type သုံးခု: `string` (စာသား)၊ `number` (ကိန်းပြည့်နဲ့ ဒသမ)၊ `boolean` (true/false)
- Annotation ပုံစံက `let name: type = value;` ပါ၊ type နာမည်ကို စာလုံးအသေးနဲ့ ရေးပါ
- Initial value ရှိရင် TypeScript က type ကို infer လုပ်ပေးပါတယ်
- `5.0` က `5` အဖြစ် print ထွက်ပါတယ်၊ ဒသမ ပြချင်ရင် `toFixed` ကို သုံးပါ
- `any` က checking ကို ပိတ်ပြီး ကူးစက်တတ်လို့ ရှောင်ပါ
TypeScript Handbook — TypeScript