ရိုးရိုးလေး ရှင်းပြမယ်
Interface ကို ရိုးရိုးလေး ရှင်းရရင်
Interface ဆိုတာ object တစ်ခု ဘယ်လို ပုံစံ ဖြစ်ရမလဲဆိုတာကို ဖော်ပြချက်တစ်ခုပါ: ဘယ် property တွေ ပါရမလဲ၊ တစ်ခုစီရဲ့ type က ဘာလဲ၊ ဘယ် method တွေ ရှိရမလဲ ဆိုတာပါ။ Label တပ်ထားတဲ့ field တွေပါတဲ့ form တစ်ခုလို မြင်ကြည့်ပါ: required field တိုင်းကို မှန်ကန်တဲ့ value အမျိုးအစားနဲ့ ဖြည့်ထားရင် object က interface နဲ့ ကိုက်ပါတယ်။
Interface ကိုယ်တိုင်က runtime မှာ ဘာမှ မလုပ်ပါဘူး။ Code ရေးနေတုန်း compiler က စစ်ပေးတဲ့ ကတိတစ်ခုပါ၊ ဒါကြောင့် User ကို လက်ခံတဲ့ function က user.name ရှိပြီး string ဖြစ်တယ်လို့ ယုံကြည်စွာ သုံးလို့ ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: ခေါင်းထဲမှာပဲ ရှိနေတဲ့ contract
User card တွေကို print ထုတ်တဲ့ function တစ်ခု ရှိတယ် ဆိုပါစို့။ တစ်နေရာမှာ ID ကို စာလုံးကြီးနဲ့ { name: "Alice", ID: 123 } လို့ ခေါ်ထားပြီး နောက်တစ်နေရာမှာ { nmae: "Bob", id: 7 } လို့ ခေါ်ထားပါတယ်။ JavaScript သက်သက်မှာ call နှစ်ခုလုံး run ပြီး undefined တွေ print ထွက်လာပါတယ်။ Customer တစ်ယောက်က screenshot ပို့လာမှပဲ အမှားကို သိရပါတယ်။
function printUser(user) { // implicitly any: nothing is checked
console.log(`User: ${user.name} (ID: ${user.id})`);
}
printUser({ name: "Alice", ID: 123 }); // User: Alice (ID: undefined)
printUser({ nmae: "Bob", id: 7 }); // User: undefined (ID: 7)ဘာကြောင့် ဘယ်သူမှ မဖမ်းမိတာလဲ
printUser က ဘယ် property တွေ လိုလဲဆိုတာ ဘယ်နေရာမှာမှ ရေးမထားတာကြောင့် editor ရော compiler ရော call ကို နှိုင်းယှဉ်စရာ မျှော်လင့်ချက် မရှိပါဘူး။ Contract က ရေးတဲ့သူရဲ့ ခေါင်းထဲမှာပဲ ရှိနေပါတယ်။ Interface က အဲဒီ contract ကို compiler စစ်လို့ရတဲ့ နေရာမှာ ချရေးပေးပြီး call တိုင်းကို အဲဒါနဲ့ တိုက်စစ်ပါတယ်။
အဓိက concept: shape ကို စစ်တဲ့ contract
Interface က object မှာ ရှိရမယ့် member တွေကို စာရင်းပြုပါတယ်။ Property တစ်ခုစီမှာ နာမည်နဲ့ type ရှိပါတယ်။ နာမည်နောက်မှာ ? ထည့်ရင် (is_admin?: boolean) optional ဖြစ်သွားပြီး type ထဲမှာ undefined ပါ ပါလာပါတယ်။ readonly ကတော့ object ဆောက်ပြီးနောက် ပြန် assign လုပ်တာကို တားပါတယ်။ Method တွေကို greet(): string လို body မပါတဲ့ signature အဖြစ် ရေးပါတယ်: interface က ဘာရှိလဲ ပြောတာဖြစ်ပြီး ဘယ်လို အလုပ်လုပ်လဲ မပြောပါဘူး။
TypeScript က interface တွေကို structural နည်းနဲ့ စစ်ပါတယ်။ Object တစ်ခုမှာ User ရဲ့ required member အားလုံး type ကိုက်ညီစွာ ရှိရင် User ကို satisfy ဖြစ်ပါတယ်၊ User လို့ ကြေညာထားဖို့ ဒါမှမဟုတ် constructor အထူးနဲ့ ဆောက်ထားဖို့ မလိုပါဘူး။ name နဲ့ id ရှိရင် User ပဲ ဆိုတဲ့ သဘောပါ။ Variable တစ်ခုကနေ ရောက်လာတဲ့ value မှာ property အပိုတွေ ပါနေရင်လည်း User အဖြစ် လက်ခံပါတယ်။
Beginner တွေကို ထောင်ချောက်ဆင်တတ်တဲ့ ခြွင်းချက်တစ်ခု ရှိပါတယ်: excess property check ပါ။ Interface မျှော်လင့်တဲ့ နေရာမှာ object literal ကို တိုက်ရိုက် ရေးလိုက်ရင် interface မသိတဲ့ property တွေကိုပါ TypeScript က ငြင်းပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ အသစ်ရေးတဲ့ literal ထဲက မသိတဲ့ property ဆိုတာ is_admin အစား isAdmin လို typo ဖြစ်တာ များလို့ပါ။ Object တူတူကို variable ထဲ အရင်ထည့်ထားရင်တော့ တခြားနေရာမှာ မှန်မှန်ကန်ကန် သုံးနေနိုင်တာကြောင့် ဖြတ်သွားခွင့် ရပါတယ်။
What the compiler accepts for printUser(user: User)
---------------------------------------------------
interface User { name: string; id: number; is_admin?: boolean }
printUser({ name: "Ann", id: 1 }) OK
printUser({ name: "Ann" }) error: id is missing
printUser({ name: "Ann", id: "1" }) error: id is a number
printUser({ name: "Ann", id: 1, isAdmin: true }) error: excess property
const u = { name: "Ann", id: 1, team: "web" }
printUser(u) OK: not a fresh literalInterface တွေက extends နဲ့ တစ်ခုပေါ် တစ်ခု ဆောက်လို့ ရပါတယ်: interface Admin extends User { permissions: string[] } မှာ User ရဲ့ member အားလုံးအပြင် permissions ပါ ပါပါတယ်။ Alias တွေလိုပဲ interface တွေလည်း compile လုပ်တဲ့အခါ ပျောက်သွားတာကြောင့် instanceof နဲ့ သုံးလို့ မရသလို runtime မှာ JSON ကို validate လုပ်ဖို့လည်း သုံးလို့ မရပါဘူး။
Key နာမည်တွေကို ကြိုမသိတဲ့ dictionary လို object တွေအတွက်တော့ index signature ကို သုံးပါတယ်: interface Scores { [player: string]: number } ဆိုရင် string key မှန်သမျှမှာ number value ရှိရပါမယ်။ scores["Aung"] = 12 က မှန်ပြီး scores["Aung"] = "twelve" ကတော့ compile error ဖြစ်ပါတယ်။ Key တွေ ပုံသေ ဖြစ်ရင်တော့ property တစ်ခုချင်းစီကို နာမည်နဲ့ ရေးတာက ပိုလုံခြုံပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ index signature က မရှိတဲ့ key ကို ဖတ်တာကိုလည်း ခွင့်ပြုလို့ပါ။
- Contract ရေး: interface User
- Optional (?) နဲ့ readonly အပိုင်း မှတ်
- Compiler က နာမည် မဟုတ်ဘဲ shape ကို စစ်
- Runtime မှာ ပျောက်: အဲဒီမှာ မစစ်ပေး
Interface က compile time မှာ shape ကို စစ်တဲ့ ကတိပါ၊ runtime validator မဟုတ်ပါဘူး။
အထွေထွေ ပုံစံ
interface Name {
required: Type;
optional?: Type; // may be missing
readonly fixed: Type; // cannot be reassigned
method(param: Type): Result;
}
interface Child extends Name {
extra: Type;
}
const value: Name = { /* every required member */ };Property တစ်ခုစီကို name: Type နဲ့ ရေးပြီး ? နဲ့ optional၊ readonly နဲ့ ပြန်မပြင်ရအောင် လုပ်ပါ။ extends က ရှိပြီးသား interface ရဲ့ member အားလုံးကို ယူပြီး အသစ်တွေ ထပ်ထည့်ပါတယ်။
User contract တစ်ခု
အောက်က example က User interface ကို ကြေညာပြီး parameter type အဖြစ် သုံးကာ contract နဲ့ ကိုက်တဲ့ object တစ်ခုကို pass လုပ်ပါတယ်။
interface User {
name: string;
id: number;
is_admin?: boolean; // Optional property
}
function printUser(user: User) {
console.log(`User: ${user.name} (ID: ${user.id})`);
}
let myUser: User = { name: "Alice", id: 123 };
printUser(myUser);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 1-5
interface User {name (string) နဲ့ id (number) ဆိုတဲ့ required property နှစ်ခုပါတဲ့ contract ကို ကြေညာပါတယ်။
- Line 4
is_admin?: boolean; // Optional property? ကြောင့် optional ဖြစ်ပါတယ်။ Line 11 က object မှာ ဒီ property မပါပေမယ့် type-check အောင်ပါတယ်။ user.is_admin ကို ဖတ်ရင် boolean | undefined ရပါတယ်။
- Line 7
function printUser(user: User) {Parameter ကို interface နဲ့ type ပေးထားတာကြောင့် function ထဲမှာ editor က name နဲ့ id ကို autocomplete လုပ်ပေးပြီး user.nmae လို typo ကို ချက်ချင်း ဖမ်းပါတယ်။
- Line 11
let myUser: User = { name: "Alice", id: 123 };Annotation က object literal ကို စစ်ပါတယ်။ ဒီမှာ isAdmin: true လို့ ထည့်ရင် excess property error ဖြစ်ပြီး id ကို ချန်ခဲ့ရင် missing property error ဖြစ်ပါမယ်။
- Line 12
printUser(myUser);myUser က User ဖြစ်ကြောင်း compiler က သိပြီးသားမို့ call က လုံခြုံပြီး User: Alice (ID: 123) ကို print ထုတ်ပါတယ်။
User: Alice (ID: 123)Extends နဲ့ interface vs type alias
Shape အခြေခံ တစ်ခုကို ထပ်ခါထပ်ခါ မရေးဘဲ extends နဲ့ ပြန်သုံးပါ။ Method signature တွေကိုလည်း property တွေနည်းတူ ကြေညာလို့ ရပြီး readonly property ကို ပြန် assign လုပ်ရင် compile error ဖြစ်ပါတယ်။
interface Entity {
readonly id: number;
createdAt: Date;
}
interface Product extends Entity {
name: string;
price: number;
discount?: number; // percent
label(): string;
}
const book: Product = {
id: 1,
createdAt: new Date("2026-01-15"),
name: "TypeScript Basics",
price: 20,
label() {
return `${this.name} - $${this.price}`;
},
};
console.log(book.label()); // TypeScript Basics - $20
// book.id = 2; // error: Cannot assign to 'id' because it is a read-only property| လိုအပ်ချက် | interface | type alias |
|---|---|---|
| Object shape ဖော်ပြဖို့ | ရတယ် | ရတယ် |
| Union, literal, tuple | မရဘူး | ရတယ် |
| Shape တစ်ခုပေါ် ဆောက်ဖို့ | extends | & (intersection) |
| ကြေညာချက် ပြန်ဖွင့်ပြီး member ထပ်ထည့် | ရတယ် (declaration merging) | မရဘူး |
လက်တွေ့မှာ object shape အတွက် ဘယ်ဟာ ရွေးရွေး ရပါတယ်၊ project တစ်ခုထဲမှာ တစ်ပြေးညီ ဖြစ်ဖို့က ပိုအရေးကြီးပါတယ်။ Object shape အတွက် interface၊ union နဲ့ တခြား အရာတွေအတွက် type alias ဆိုတဲ့ စည်းမျဉ်းကို team အများအပြား သုံးကြပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project: dependency တွေအတွက် contract
လက်တွေ့ code base တွေမှာ interface တွေကို နေရာနှစ်ခုမှာ အများဆုံး တွေ့ရပါတယ်။ ပထမ — frontend နဲ့ backend ကြား API contract ပါ: OrderResponse လို interface တွေကို shared package တစ်ခုထဲ ထားပြီး client ရော server ရော import လုပ်ကြပါတယ်။ ဒုတိယ — service တစ်ခုက မှီခိုတဲ့ dependency တွေအတွက် contract ပါ။ Checkout code က payment provider အစစ်ကို တိုက်ရိုက် မသိဘဲ PaymentGateway interface ကိုပဲ သိပါတယ်။ Structural typing ကြောင့် test ထဲမှာ အဲဒီ shape နဲ့ ကိုက်တဲ့ fake object တစ်ခုကို ထည့်ပေးရုံပါပဲ:
interface PaymentGateway {
charge(amountCents: number, customerId: string): Promise<string>;
}
const fakeGateway: PaymentGateway = {
async charge(amountCents, customerId) {
return `fake-receipt-${customerId}-${amountCents}`;
},
};
async function checkout(gateway: PaymentGateway, total: number): Promise<void> {
const receipt = await gateway.charge(total, "cus_42");
console.log("Receipt:", receipt);
}
checkout(fakeGateway, 1999); // Receipt: fake-receipt-cus_42-1999Production မှာတော့ provider ရဲ့ SDK ကို ခေါ်တဲ့ object အစစ်ကို ထည့်ပေးပါတယ်၊ checkout function ကို တစ်လိုင်းမှ ပြင်စရာ မလိုပါဘူး။ Provider ပြောင်းချင်တဲ့နေ့မှာလည်း interface နဲ့ ကိုက်တဲ့ object အသစ်တစ်ခု ရေးရုံပါပဲ။ Interface မှာ method အသစ်တစ်ခု ထပ်ထည့်ရင် အဲဒီ method မပါသေးတဲ့ implementation တိုင်းမှာ compiler က error ပြပေးတာကြောင့် ဘယ်နေရာကို ပြင်ရမလဲ ချက်ချင်း သိရပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားများ
1. Excess property error ကို as နဲ့ ဖုံးတာ
"Object literal may only specify known properties" error ကို မြင်ရင် { ... } as User လို့ ရေးပြီး ပိတ်လိုက်ချင်စိတ် ပေါ်ပါတယ်။ ဒီ error က များသောအားဖြင့် isAdmin နဲ့ is_admin လို typo ကို ညွှန်ပြနေတာပါ။ Property နာမည်ကို ပြင်ပါ၊ ဒါမှမဟုတ် property က တကယ် လိုအပ်ရင် interface ထဲ ထည့်ပါ။
2. Optional property ကို အမြဲရှိတယ်လို့ သဘောထားတာ
discount?: number ဆိုရင် price * (1 - product.discount / 100) ကို strict mode က ငြင်းပါတယ်၊ discount က undefined ဖြစ်နိုင်လို့ပါ။ JavaScript မှာ အဲဒီ တွက်ချက်မှုက NaN ပြန်ပေးပါလိမ့်မယ်။ product.discount ?? 0 လို default တစ်ခု ပေးပါ။
3. Interface က runtime data ကို စစ်ပေးမယ်လို့ ထင်တာ
const user: User = await res.json() က compile ဖြစ်ပါတယ်၊ json() က any ပြန်ပေးလို့ပါ။ ဘာမှ စစ်မထားပါဘူး။ ပြင်ပ data ကို ယခင် lesson က type guard လို runtime check နဲ့ validate လုပ်ပါ။ readonly ကလည်း အပေါ်ယံပဲ ကာကွယ်ပါတယ်: readonly tags: string[] မှာ tags.push() ကို ဆက်ခေါ်လို့ ရပါသေးတယ်။
Best Practices
အကောင်းဆုံး လုပ်ထုံးများ
- Interface ကို လိုအပ်တဲ့ member တွေလောက်ပဲ ထားပါ၊ function တစ်ခုက name ပဲ လိုရင် object တစ်ခုလုံးကို မတောင်းပါနဲ့။
- ID နဲ့ createdAt လို ဆောက်ပြီးနောက် မပြောင်းသင့်တဲ့ field တွေကို readonly လုပ်ပါ။
- Optional property တွေကို ဖတ်တဲ့အခါ ?? ဒါမှမဟုတ် check တစ်ခုနဲ့ default ပေးပါ။
- Object shape တွေအတွက် interface နဲ့ union တွေအတွက် type alias ဆိုတဲ့ စည်းမျဉ်းကို project တစ်ခုလုံး တစ်ပြေးညီ လိုက်နာပါ။
Mini challenge
စာကြည့်တိုက် စာအုပ်စင်
စာအုပ်တွေအတွက် interface တစ်ခုနဲ့ အဲဒါကို extend လုပ်တဲ့ e-book interface တစ်ခု ရေးပြီး စာအုပ်စင်ပေါ်က စာအုပ်တွေကို ဖော်ပြကာ စာမျက်နှာ စုစုပေါင်းကို တွက်ပါ။
- Book interface မှာ readonly isbn, title, author, pages နဲ့ optional borrowedBy ပါရပါမယ်။
- EBook က Book ကို extend လုပ်ပြီး fileSizeMb: number ကို ထပ်ထည့်ရပါမယ်။
- describeBook(book) က "Title by Author (N pages) - available" ဒါမှမဟုတ် "- borrowed by Name" ကို ပြန်ပေးရပါမယ်။
- totalPages(books) က pages အားလုံးကို ပေါင်းပေးရပါမယ်။
- EBook တစ်အုပ်ကို Book[] array ထဲ ထည့်ပြီး structural compatibility ကို ပြပါ။
// TODO: interface Book (readonly isbn, title, author, pages, optional borrowedBy)
// TODO: interface EBook extends Book (adds fileSizeMb)
function describeBook(book: Book): string {
// TODO: "available" when nobody borrowed it, else "borrowed by <name>"
return "";
}
function totalPages(books: Book[]): number {
// TODO: add up the pages
return 0;
}
// TODO: create one Book and one EBook, put both in a Book[] and print the resultsရလဒ်: "Dune by Frank Herbert (412 pages) - available"၊ "Learning TS by Mya Mya (250 pages) - borrowed by Ko Aung" နဲ့ "Total pages: 662" ဆိုတဲ့ line သုံးကြောင်း ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
book.borrowedBy က string | undefined ဖြစ်တာကြောင့် truthiness check တစ်ခုနဲ့ status ကို ရွေးလို့ ရပါတယ်။ totalPages အတွက် reduce((sum, b) => sum + b.pages, 0) ကို သုံးကြည့်ပါ။
အဖြေ ကြည့်ရန်
interface Book {
readonly isbn: string;
title: string;
author: string;
pages: number;
borrowedBy?: string;
}
interface EBook extends Book {
fileSizeMb: number;
}
function describeBook(book: Book): string {
const status = book.borrowedBy ? `borrowed by ${book.borrowedBy}` : "available";
return `${book.title} by ${book.author} (${book.pages} pages) - ${status}`;
}
function totalPages(books: Book[]): number {
return books.reduce((sum, b) => sum + b.pages, 0);
}
const novel: Book = { isbn: "978-1", title: "Dune", author: "Frank Herbert", pages: 412 };
const guide: EBook = {
isbn: "978-2",
title: "Learning TS",
author: "Mya Mya",
pages: 250,
borrowedBy: "Ko Aung",
fileSizeMb: 4.2,
};
const shelf: Book[] = [novel, guide]; // an EBook is also a Book
shelf.forEach((b) => console.log(describeBook(b)));
console.log(`Total pages: ${totalPages(shelf)}`);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Interface က object ရဲ့ property, type နဲ့ method တွေကို compiler စစ်လို့ရတဲ့ contract အဖြစ် ဖော်ပြပါတယ်။
- ? က property ကို optional ဖြစ်စေပြီး readonly က ပြန် assign လုပ်တာကို (shallow အနေနဲ့) တားပါတယ်။
- TypeScript က shape ကို စစ်တာဖြစ်ပြီး fresh object literal တွေမှာပဲ excess property check လုပ်ပါတယ်။
- extends က interface တွေကို ပြန်သုံးစေပြီး interface တွေက runtime မှာ ပျောက်သွားပါတယ်။
TypeScript Handbook — TypeScript