ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Function တစ်ခုကို စက်ရုံက စက်တစ်လုံးလို့ မြင်ကြည့်ပါ။ အဝင်ပေါက် (parameter) တွေမှာ ဘာထည့်ရမလဲ၊ အထွက်ပေါက် (return value) ကနေ ဘာထွက်လာမလဲဆိုတာ စက်ပေါ်မှာ တံဆိပ်ကပ်ထားပါတယ်။ TypeScript မှာ အဲဒီ တံဆိပ်တွေက type annotation တွေပါ — function add(a: number, b: number): number ဆိုတာ "number နှစ်ခု ထည့်ပါ၊ number တစ်ခု ပြန်ရပါမယ်" လို့ ပြောတာပါ။
တံဆိပ်ရှိနေတော့ မှားထည့်ရင် (string ထည့်တာ၊ argument တစ်ခု ကျန်ခဲ့တာ) code ကို run မလုပ်ခင်ကတည်းက editor က မျဉ်းနီ ပြပေးပါတယ်။ Function ကို ရေးတဲ့သူဘက်ကလည်း ကတိပေးထားတဲ့ type ကို အမြဲ return ပြန်ရပါမယ်။
Problem → Why → Solution
ပြဿနာ: argument မှားပို့တာကို JavaScript က မသိ
Plain JavaScript မှာ function တစ်ခုက ဘာ argument လိုချင်လဲဆိုတာ နာမည်ကနေ ခန့်မှန်းရုံပဲ ရှိပါတယ်။ အောက်က formatPrice() ကို argument အစဉ် ပြောင်းပြန် ခေါ်လိုက်တော့ runtime ကျမှ crash ဖြစ်ပါတယ်။ getDiscount() ကတော့ ပိုဆိုးပါတယ် — code မသိတဲ့ coupon ဆိုရင် undefined ကို return ပြန်ပြီး ဈေးနှုန်းတွက်ချက်မှု တစ်ခုလုံး NaN ဖြစ်သွားပါတယ်၊ error message တစ်ခုမှ မပေါ်ပါဘူး။
// Plain JavaScript: no types on parameters or return values
function formatPrice(amount, currency) {
return amount.toFixed(2) + " " + currency;
}
function getDiscount(code) {
if (code === "SAVE10") return 10;
// forgot the other codes: returns undefined
}
formatPrice("USD", 9.5); // TypeError: amount.toFixed is not a function
const finalPrice = 50 - getDiscount("WELCOME"); // NaNဘာကြောင့် ဒီ bug တွေ လွတ်သွားတာလဲ
JavaScript က argument အရေအတွက်ကိုလည်း type ကိုလည်း မစစ်ပါဘူး၊ function က ဘာ return ပြန်ရမယ်ဆိုတဲ့ ကတိလည်း မရှိပါဘူး။ TypeScript မှာ formatPrice(amount: number, currency: string) လို့ ရေးလိုက်ရင် အစဉ်မှားခေါ်တာ compile error ဖြစ်ပြီး getDiscount(code: string): number လို့ ရေးလိုက်ရင် return မပြန်တဲ့ လမ်းကြောင်းကိုလည်း compiler က ဖမ်းပေးပါတယ်။
Core concept: function signature က ကတိတစ်ခုပါ
Function ရဲ့ signature ဆိုတာ နာမည်၊ parameter list နဲ့ return type ပေါင်းထားတာပါ။ ဒါက ဘက်နှစ်ဖက်ကို ချည်နှောင်ထားတဲ့ ကတိတစ်ခုပါ — caller က မှန်ကန်တဲ့ argument တွေကို မှန်ကန်တဲ့ အရေအတွက်နဲ့ ပို့ရပြီး function body ကလည်း ကတိပေးထားတဲ့ type ကို လမ်းကြောင်းတိုင်းမှာ return ပြန်ရပါမယ်။ ဘယ်ဘက်က ချိုးဖောက်ချိုးဖောက် compiler က error ပြပါတယ်။
Parameter တွေကို အမြဲ type ပေးရပါမယ်။ strict mode (noImplicitAny) မှာ function add(a, b) လို ရေးရင် "Parameter 'a' implicitly has an 'any' type" error တက်ပါတယ်၊ TypeScript က parameter type ကို body ကနေ ခန့်မှန်းမပေးလို့ပါ။ Return type ကတော့ ကွာပါတယ် — return statement တွေကနေ compiler က infer လုပ်ပေးနိုင်ပါတယ်။ ဒါပေမယ့် export လုပ်တဲ့ function တွေနဲ့ အရေးကြီးတဲ့ logic တွေမှာ return type ကို ကိုယ်တိုင် ရေးထားတာ ကောင်းပါတယ်၊ body ထဲမှာ return တစ်ခု မှားသွားရင် function ထဲမှာပဲ error ပြမှာဖြစ်ပြီး caller ဆယ်နေရာမှာ ပျံ့မသွားလို့ပါ။
void ဆိုတာ "ဒီ function ကနေ အသုံးဝင်တဲ့ value ပြန်မလာဘူး" လို့ ဆိုလိုပါတယ်။ console.log() ခေါ်တာ၊ file ရေးတာလို side effect အတွက်ပဲ ရှိတဲ့ function တွေမှာ သုံးပါတယ်။ void function ရဲ့ ရလဒ်ကို if condition ထဲမှာ စစ်တာ၊ number variable ထဲ assign လုပ်တာလို တကယ့် value အဖြစ် သုံးဖို့ ကြိုးစားရင် compiler က error ပြပါတယ်။
Argument အရေအတွက်ကိုလည်း compiler က စစ်ပါတယ်။ ပိုပို့ရင်ရော လျော့ပို့ရင်ပါ "Expected 2 arguments, but got 1" လို error ပေးပါတယ်။ Argument တစ်ခုကို မဖြစ်မနေ မလိုရင် optional (?) ဒါမှမဟုတ် default value ပေးရပါမယ် — အဲဒါကို အောက်က deeper section မှာ ကြည့်ပါမယ်။
Arrow function တွေမှာလည်း စည်းမျဉ်း အတူတူပါပဲ — const double = (n: number): number => n * 2; လို့ ရေးပါတယ်။ function keyword နဲ့ ရေးရေး၊ arrow နဲ့ ရေးရေး TypeScript အတွက် signature က အရေးကြီးတာပါ။ Type တွေက compile time မှာပဲ ရှိပြီး JavaScript ထွက်လာတဲ့အခါ ဖျက်ပစ်လိုက်တာမို့ runtime မှာ argument တွေကို ထပ်စစ်ပေးတာ မဟုတ်ပါဘူး။
- Caller က argument တွေ ပို့
- Parameter type နဲ့ တစ်ခုချင်း စစ်
- Body က ကတိထားတဲ့ type ကို return
- Caller က type သိပြီးသား ရလဒ် ရ
Parameter type က caller ကို စစ်ပြီး return type က function body ကို စစ်ပါတယ်။
အထွေထွေ ပုံစံ
function name(param: Type, optional?: Type): ReturnType {
return value;
}
const arrowName = (param: Type): ReturnType => value;
type Callback = (param: Type) => ReturnType;Parameter တစ်ခုစီ နောက်မှာ : Type ရေးပြီး parameter list ရဲ့ နောက်မှာ : ReturnType ရေးပါတယ်။ Arrow function မှာလည်း ပုံစံ အတူတူပါပဲ။ type Callback = (...) => ... ကတော့ function တစ်ခုရဲ့ ပုံသဏ္ဌာန်ကို type အဖြစ် နာမည်ပေးတာပါ။
Example: value ပြန်တဲ့ function နဲ့ void function
ဒီ example မှာ function နှစ်မျိုး ပါပါတယ်။ add() က number တစ်ခု return ပြန်ပြီး logMessage() က print ထုတ်ရုံပဲ လုပ်လို့ void ပါ။ ပထမ function ရဲ့ ရလဒ်ကို ဒုတိယ function ဆီ ပို့ထားတာကို သတိထားကြည့်ပါ။
// This function takes two numbers and returns a number
function add(a: number, b: number): number {
return a + b;
}
// This function doesn't return anything (void)
function logMessage(message: string): void {
console.log(message);
}
let sum: number = add(5, 10);
logMessage("The sum is " + sum);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 2
function add(a: number, b: number): number {a နဲ့ b နှစ်ခုလုံး number ဖြစ်ရပြီး ) နောက်က : number က return type ပါ။ add("5", 10) လို ခေါ်ရင် compile error တက်ပါတယ်။
- Line 3
return a + b;number နှစ်ခု ပေါင်းရင် number ရလို့ ကတိနဲ့ ကိုက်ပါတယ်။ ဒီနေရာမှာ string တစ်ခု return ပြန်မိရင် error က ဒီ line မှာပဲ ပေါ်ပါမယ်။
- Line 7
function logMessage(message: string): void {void က ဒီ function ကနေ အသုံးဝင်တဲ့ value ပြန်မလာဘူးလို့ ပြောပါတယ်။ အလုပ်က console.log() ခေါ်ရုံပါပဲ။
- Line 11
let sum: number = add(5, 10);add() ရဲ့ return type က number ဖြစ်ပြီးသားမို့ : number annotation က မလိုအပ်ပါဘူး၊ const sum = add(5, 10); လို့ ရေးလည်း type အတူတူ ရပါတယ်။
- Line 12
logMessage("The sum is " + sum);string နဲ့ number ပေါင်းရင် string ရလို့ message: string နဲ့ ကိုက်ပါတယ်။ Output က "The sum is 15" ပါ။
The sum is 15Optional, default နဲ့ rest parameters
Parameter နာမည်နောက်မှာ ? ထည့်ရင် optional ဖြစ်ပြီး function ထဲမှာ အဲဒီ parameter ရဲ့ type က string | undefined ဖြစ်ပါတယ်။ Default value ပေးထားရင်လည်း optional ဖြစ်ပေမယ့် body ထဲမှာ undefined ဖြစ်နိုင်ခြေ မရှိတော့ပါဘူး၊ type ကိုလည်း default value ကနေ infer လုပ်ပါတယ်။ Rest parameter (...prices: number[]) ကတော့ argument အရေအတွက် မကန့်သတ်ဘဲ array တစ်ခုအဖြစ် စုပေးပါတယ်။ Optional နဲ့ rest parameter တွေက လိုအပ်တဲ့ parameter တွေရဲ့ နောက်မှာ ရှိရပါမယ်။
function greet(name: string, greeting?: string): string {
return `${greeting ?? "Hello"}, ${name}!`;
}
function repeat(text: string, times = 2): string {
return text.repeat(times);
}
function total(...prices: number[]): number {
return prices.reduce((sum, price) => sum + price, 0);
}
console.log(greet("Aye"));
console.log(greet("Aye", "Mingalaba"));
console.log(repeat("ha"));
console.log(total(1.5, 2, 3.5));Function type နဲ့ callback
JavaScript မှာ function တွေကို value တွေလို တခြား function ဆီ ပို့လို့ ရပါတယ်။ အဲဒီ callback ကို type ပေးဖို့ (amount: number) => string လို function type expression ကို သုံးပါတယ်။ Formatter type ကို နာမည်ပေးထားရင် callback ဆီမှာ parameter type ပြန်ရေးစရာ မလိုပါဘူး — TypeScript က context ကနေ amount ဟာ number ဖြစ်ကြောင်း သိပါတယ်။
type Formatter = (amount: number) => string;
const asDollars: Formatter = (amount) => "$" + amount.toFixed(2);
const asKyat: Formatter = (amount) => `${Math.round(amount)} MMK`;
function printAll(amounts: number[], format: Formatter): void {
for (const amount of amounts) console.log(format(amount));
}
printAll([3.5, 10], asDollars);
printAll([2500.4], asKyat);လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Real project တွေမှာ function signature တွေက team တစ်ခုလုံးအတွက် documentation ဖြစ်လာပါတယ်။ calculateShipping(weightKg: number, zone: string): number လို signature ကို ဖတ်လိုက်တာနဲ့ implementation ကို မဖွင့်ကြည့်ဘဲ ဘယ်လို ခေါ်ရမလဲ သိပါတယ်။ Editor ကလည်း hover လုပ်တဲ့အခါ အဲဒီ signature ကို ပြပြီး autocomplete ပေးပါတယ်။
Callback type တွေက နေရာတိုင်းမှာ ရှိပါတယ် — array.map() နဲ့ filter() ဆီ ပို့တဲ့ function၊ Express route handler (req, res) => void၊ React ရဲ့ onClick handler နဲ့ setTimeout callback တွေပါ။ Library တွေက ဒီ function type တွေကို ကြိုသတ်မှတ်ထားလို့ ကိုယ့် callback ရဲ့ parameter type တွေကို ရေးစရာ မလိုဘဲ TypeScript က ဖြည့်ပေးပါတယ်။ Refactor လုပ်တဲ့အခါ parameter တစ်ခု ထပ်ထည့်တာ ဒါမှမဟုတ် return type ပြောင်းတာနဲ့ ခေါ်ထားတဲ့ နေရာတိုင်းကို compiler က ပြပေးလို့ project ကြီးတွေမှာ ပြောင်းလဲမှုတွေ လုပ်ရတာ ဘေးကင်းပါတယ်။ ဒါပေမယ့် form ဒါမှမဟုတ် API လို program အပြင်ကနေ ဝင်လာတဲ့ data ကိုတော့ ဒီ type တွေက runtime မှာ မစစ်ပေးပါဘူး၊ ဒါကြောင့် type ပေးထားတဲ့ function တွေဆီ မရောက်ခင် boundary မှာ validate လုပ်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. လမ်းကြောင်းတစ်ခုမှာ return မေ့တာ
function isAdult(age: number): boolean { if (age >= 18) return true; } က "Function lacks ending return statement and return type does not include 'undefined'" error ပေးပါတယ်။ Return type ရေးထားလို့ ဖမ်းမိတာပါ — return type မရေးရင် boolean | undefined လို့ infer လုပ်ပြီး ပြဿနာက caller ဆီ ရွှေ့သွားပါတယ်။ return age >= 18; လို့ ရေးပါ။
2. Optional parameter ကို မစစ်ဘဲ သုံးတာ
currency?: string ဆိုရင် function ထဲမှာ currency က string | undefined ပါ။ currency.toUpperCase() ကို တိုက်ရိုက် ခေါ်ရင် "'currency' is possibly 'undefined'" error တက်ပါတယ်။ ?? "USD" နဲ့ fallback ပေးတာ ဒါမှမဟုတ် default value currency = "USD" သုံးတာက ပိုရှင်းပါတယ်။
3. Callback ကို Function သို့မဟုတ် any နဲ့ type ပေးတာ
onDone: Function ဆိုရင် ဘယ် argument နဲ့မဆို ခေါ်လို့ရပြီး ရလဒ်က any ဖြစ်ပါတယ်။ (result: string) => void လို တိကျတဲ့ function type ကို သုံးပါ၊ ဒါမှ caller ဘက်ကရော callback ဘက်ကပါ စစ်ဆေးမှု ရပါမယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Export လုပ်တဲ့ function တိုင်းမှာ return type ကို ရေးထားပါ၊ local helper သေးသေးလေးတွေမှာတော့ inference ကို ယုံလို့ရပါတယ်။
- Boolean parameter သုံးလေးခု ဆက်တိုက် ရှိနေရင် ဘယ်ဟာ ဘာလဲ မှတ်ရခက်လို့ options object တစ်ခုနဲ့ အစားထိုးပါ။
- Optional parameter နဲ့ undefined စစ်တာထက် default value က ပိုရှင်းရင် default ကို ရွေးပါ။
Mini challenge
Mini challenge: ဈေးနှုန်း helper များ
Online shop တစ်ခုအတွက် type ပြည့်စုံတဲ့ ဈေးနှုန်း function သုံးခု ရေးပြီး ပေါင်းသုံးကြည့်ပါ။
- priceWithTax(price, taxRate) — taxRate ရဲ့ default က 0.05 ဖြစ်ပြီး ရလဒ်ကို ဒသမ နှစ်နေရာ round လုပ်ရမယ်
- formatPrice(amount, currency?) — currency မပါရင် "USD" သုံးရမယ်
- applyAll(price, ...steps) — (value: number) => number function တွေကို အစဉ်လိုက် apply လုပ်ရမယ်
- Parameter နဲ့ return type အားလုံးကို ရေးရမယ်၊ any မသုံးရ
function priceWithTax(/* TODO: price, taxRate with a default */): number {
// TODO: add tax and round to 2 decimal places
}
function formatPrice(/* TODO: amount, optional currency */): string {
// TODO: "21.00 USD"
}
function applyAll(/* TODO: price, ...steps */): number {
// TODO: run every step on the running result
}
// TODO: print priceWithTax(20), a 10% EUR price, and 40 half-off then taxedရလဒ်: 21.00 USD 22.00 EUR 21.00 USD
အရိပ်အမြွက် ကြည့်ရန်
ဒသမ နှစ်နေရာ round လုပ်ဖို့ Math.round(x * 100) / 100 ကို သုံးပါ။ Rest parameter ရဲ့ type က function array ဖြစ်လို့ ((value: number) => number)[] လို့ ကွင်းခတ်ပြီး ရေးရပါတယ်။
အဖြေ ကြည့်ရန်
function priceWithTax(price: number, taxRate = 0.05): number {
return Math.round(price * (1 + taxRate) * 100) / 100;
}
function formatPrice(amount: number, currency?: string): string {
return `${amount.toFixed(2)} ${currency ?? "USD"}`;
}
function applyAll(price: number, ...steps: ((value: number) => number)[]): number {
let result = price;
for (const step of steps) result = step(result);
return result;
}
const halfOff = (value: number): number => value / 2;
console.log(formatPrice(priceWithTax(20)));
console.log(formatPrice(priceWithTax(20, 0.1), "EUR"));
console.log(formatPrice(applyAll(40, halfOff, (v) => priceWithTax(v))));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Function signature က caller နဲ့ body နှစ်ဖက်လုံးကို ချည်နှောင်တဲ့ ကတိပါ — parameter type တွေက caller ကို၊ return type က body ကို စစ်ပါတယ်။
- Parameter တွေကို အမြဲ type ပေးပါ၊ return type ကို infer လုပ်ခိုင်းလို့ရပေမယ့် export လုပ်တဲ့ function တွေမှာ ရေးထားပါ။
- void က အသုံးဝင်တဲ့ value မပြန်ဘူးလို့ ဆိုလိုပြီး ? / default / ... တို့က parameter တွေကို ပိုပြောင်းလွယ်စေပါတယ်။
- (param: Type) => Result ပုံစံနဲ့ callback တွေကို type ပေးပါ၊ Function သို့မဟုတ် any မသုံးပါနဲ့။
TypeScript Handbook — TypeScript