Thuta Learning
TypeScript
ExercisesProgrammingအလယ်အလတ်

Practice Exercises - Set 1 (Basics)

TypeScriptသင်ခန်းစာ 2730 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Parameter နဲ့ return type ပါတဲ့ ဂဏန်းတွက် function တစ်ခုကို ရေးပြီး သိထားတဲ့ value တွေနဲ့ စစ်နိုင်ဖို့
  • Interface တစ်ခုနဲ့ အဲဒီ shape ကိုက်တဲ့ object array ကို ဖန်တီးနိုင်ဖို့
  • filter() က element type ကို မပြောင်းဘဲ map() က ပြောင်းတာကို ရှင်းပြပြီး ချိတ်ဆက်သုံးနိုင်ဖို့
  • Compiler error message ကို ဖတ်ပြီး ဘယ် type က ဘယ် type နဲ့ မကိုက်လဲ ရှာနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

ဒီ lesson မှာ concept အသစ် မသင်ပါဘူး။ Basic types၊ function နဲ့ interface lesson တွေမှာ ဖတ်ခဲ့တာတွေကို လက်နဲ့ ကိုယ်တိုင် ရေးကြည့်ဖို့ပါ။ ဘောလုံးကန်နည်းကို video ကြည့်ရုံနဲ့ မတတ်သလို syntax ကိုလည်း ဖတ်ရုံနဲ့ မမှတ်မိပါဘူး။ ကိုယ်တိုင်ရေး၊ error ရ၊ error ကိုဖတ်ပြီး ပြင်တဲ့ အကြိမ်တိုင်းမှာ ပိုမှတ်မိလာပါတယ်။

အစီအစဉ်က ရိုးရှင်းပါတယ်။ ပထမ Movie data နဲ့ ဖြေပြီးသား warm-up တစ်ခုကို ဖတ်ပါ။ ပြီးရင် ပုံစံတူပေမယ့် data မတူတဲ့ Book exercise သုံးခုကို ကိုယ်တိုင် ဖြေပါ။ Solution ကို မကြည့်ခင် အနည်းဆုံး တစ်ကြိမ် ကိုယ်တိုင် run ကြည့်ပါ။

Problem → Why → Solution

ပြဿနာ: compile ဖြစ်ပေမယ့် မှားနေတဲ့ code

Exercise 3 ကို ပထမဆုံး ကြိုးစားတဲ့သူ အများစုက ဒီလို ရေးတတ်ပါတယ်။ Parameter မှာ type ရှိလို့ strict mode မှာ compile ဖြစ်ပါတယ်။ Return type ကို မရေးထားလို့ TypeScript က filter() ရဲ့ ရလဒ်ကနေ Book[] လို့ ခန့်မှန်းယူပါတယ်။ ဒါပေမယ့် exercise က title string တွေကို လိုချင်တာပါ။

Bug က function ထဲမှာ ရှိပေမယ့် လက္ခဏာက တခြားနေရာမှာ ပေါ်ပါတယ်။ join(", ") က object တွေကို စာသားပြောင်းလိုက်လို့ screen ပေါ်မှာ "[object Object]" ဆိုတာ ထွက်လာပါတယ်။ Error မရှိလို့ ဘယ်နေရာ မှားလဲ ရှာရ ခက်ပါတယ်။

attempt.ts
function getAvailableBooks(books: Book[]) {
  return books.filter((b) => b.isAvailable); // forgot map(): this is Book[]
}

const titles = getAvailableBooks(books);
console.log(titles.join(", ")); // [object Object], [object Object]

Return type annotation က ဘာကြောင့် ကူညီတာလဲ

Return type ကို မရေးရင် "function ဘာပြန်ပေးလဲ" ကို code ကိုယ်တိုင်က ဆုံးဖြတ်ပါတယ်၊ မှားနေတဲ့ code ဆိုရင်လည်း မှားနေတဲ့ type ပဲ ထွက်လာပါတယ်။ : string[] လို့ ရေးထားရင် ကိုယ်လိုချင်တာကို compiler ကို ကြိုပြောထားတာ ဖြစ်လို့ "Type 'Book[]' is not assignable to type 'string[]'" error က bug ရှိတဲ့ line မှာပဲ တန်းပေါ်ပါတယ်။ (Return type ကို မရေးတာက implicit any မဖြစ်ပါဘူး၊ inferred type ဖြစ်ပါတယ်။ implicit any error ကတော့ type မပါတဲ့ parameter တွေအတွက်ပါ။)

Exercise တွေမှာ လိုမယ့် အချက်လေးချက်

Exercise တစ်ခုစီကို မစခင် function signature ကို အရင်ဖတ်ပါ။ Signature က spec ပါပဲ။ celsiusToFahrenheit(celsius: number): number ဆိုတာ "number တစ်ခုဝင်ပြီး number တစ်ခုထွက်ရမယ်" လို့ ပြောနေတာပါ။ အောက်က အချက်လေးချက်ကို နားလည်ရင် exercise သုံးခုလုံး ဖြေနိုင်ပါတယ်။

Typed parameter နဲ့ return

Parameter type က caller ကို ကာကွယ်ပြီး return type က function ကိုယ်တိုင်ကို ကာကွယ်ပါတယ်။ Strict mode မှာ type မပါတဲ့ parameter က error ဖြစ်ပြီး return type ကတော့ မရေးရင် ခန့်မှန်းယူပါတယ်။

JavaScript ဂဏန်းတွက်ချက်ပုံ

TypeScript မှာ integer နဲ့ float ခွဲမထားဘဲ number တစ်မျိုးပဲ ရှိလို့ 9 / 5 က 1 မဟုတ်ဘဲ 1.8 ပါ။ ဒါကြောင့် celsius * 9 / 5 + 32 လို့ တိုက်ရိုက် ရေးလို့ရပါတယ်။ Floating-point ကြောင့် ရလဒ်မှာ ဒသမ အများကြီး ပါလာနိုင်လို့ ပြတဲ့အခါ toFixed() သုံးပါ။

Interface နဲ့ object literal

const books: Book[] = [...] လို့ ရေးထားရင် object တစ်ခုစီကို Book နဲ့ တိုက်စစ်ပါတယ်။ Field တစ်ခု ကျန်ရင် "Property 'year' is missing"၊ နာမည်မှားရင် "Object literal may only specify known properties" error ရပါတယ်။

filter() နဲ့ map() ရဲ့ type

filter() က element တွေကို ရွေးချန်ပေမယ့် type ကို မပြောင်းပါဘူး၊ Book[] ဝင်ရင် Book[] ထွက်ပါတယ်။ map() က element တစ်ခုစီကို အသစ်တစ်ခုအဖြစ် ပြောင်းလို့ (b) => b.title ဆိုရင် string[] ထွက်ပါတယ်။ "ဘယ်ဟာတွေလဲ" ကို filter နဲ့၊ "ဘာအဖြစ် ပြောင်းမလဲ" ကို map နဲ့ ဖြေပါ။

MethodBook[] ပေါ်မှာ ခေါ်ရင် ပြန်ပေးတဲ့ type
filter((b) => b.isAvailable)Book[]
map((b) => b.title)string[]
find((b) => b.year > 2020)Book | undefined

Compiler က error ပြတဲ့အခါ message ကို အပေါ်ဆုံး line ကနေ စဖတ်ပါ။ ပထမ line က များသောအားဖြင့် "Type 'A' is not assignable to type 'B'" ပုံစံ ဖြစ်ပါတယ်။ A က ကိုယ့် code က တကယ်ထုတ်ပေးလိုက်တဲ့ type၊ B က annotation မှာ ကတိပေးထားတဲ့ type ပါ။ အောက်က indent လုပ်ထားတဲ့ line တွေက မကိုက်တဲ့နေရာကို property တစ်ခုအထိ ကျဉ်းပေးပါတယ်။ File နဲ့ line နံပါတ်ကို နှိပ်ပြီး အဲဒီ line က value ကို ကြည့်ပါ။ ပြီးရင် "A နဲ့ B ထဲက ဘယ်ဟာကို ငါတကယ် ဆိုလိုခဲ့တာလဲ" လို့ မေးပါ။ B မှန်ရင် code ကို ပြင်ပါ၊ B ကိုယ်တိုင် မှားနေမှသာ annotation ကို ပြင်ပါ။

Mental model: signature က spec
  1. Signature ကိုဖတ်ပြီး in/out ကို ခန့်မှန်း
  2. Data တွေကို interface နဲ့ စစ်
  3. filter နဲ့ ရွေး၊ map နဲ့ ပြောင်း
  4. သိထားတဲ့ value နဲ့ run ပြီး စစ်

Compile ဖြစ်တာက type မှန်တာကို ပြတာပါ၊ logic မှန်တာကို မပြပါဘူး။ return 0; လို placeholder တွေလည်း compile ဖြစ်ပါတယ်။

အခြေခံ ပုံစံ

typescript
function convert(value: number): number {
  return value * factor + offset;
}

interface Thing { name: string; active: boolean }

function activeNames(list: Thing[]): string[] {
  return list.filter((t) => t.active).map((t) => t.name);
}

Input/output type ပါတဲ့ function၊ data shape အတွက် interface၊ ပြီးရင် "ရွေး ပြီးမှ ပြောင်း" ဆိုတဲ့ filter + map chain ပါ။

ဖြေပြီးသား warm-up: Movie data

ဒီ warm-up က exercise သုံးခုနဲ့ ပုံစံတူပေမယ့် data မတူပါဘူး။ ကီလိုမီတာကို မိုင်ပြောင်းတဲ့ function၊ Movie interface နဲ့ array၊ မကြည့်ရသေးတဲ့ ရုပ်ရှင်နာမည်တွေကို ပြန်ပေးတဲ့ filter + map chain ပါ။ ဖတ်ပြီးရင် ဖိုင်ကိုပိတ်ပြီး Book exercise တွေကို မှတ်ဉာဏ်နဲ့ ရေးကြည့်ပါ။

warm-up.ts
// Warm-up 1: typed input, typed output
function kmToMiles(km: number): number {
  return km * 0.621371;
}

// Warm-up 2: an interface and a typed array
interface Movie {
  title: string;
  year: number;
  minutes: number;
  watched: boolean;
}

const movies: Movie[] = [
  { title: "Spirited Away", year: 2001, minutes: 125, watched: true },
  { title: "Inception", year: 2010, minutes: 148, watched: false },
  { title: "Coco", year: 2017, minutes: 105, watched: false },
];

// Warm-up 3: filter() keeps Movie objects, map() turns them into strings
function getUnwatchedTitles(list: Movie[]): string[] {
  return list.filter((m) => !m.watched).map((m) => m.title);
}

console.log(kmToMiles(10).toFixed(2));
console.log(getUnwatchedTitles(movies));

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Typed in, typed outfunction kmToMiles(km: number): number {

    kmToMiles("10") ဆိုရင် compile error ရပြီး return statement က number မဟုတ်ရင်လည်း error ရပါတယ်။ Exercise 1 က ဒီပုံစံအတိုင်းပါ။

  2. Typed array literalconst movies: Movie[] = [

    Array ထဲက object တိုင်းကို Movie နဲ့ စစ်ပါတယ်။ minutes ကို မထည့်ရင် ဒါမှမဟုတ် "125" လို string ပေးရင် error ရပါတယ်။

  3. Choose, then reshapereturn list.filter((m) => !m.watched).map((m) => m.title);

    filter() က Movie[] ကိုပဲ ပြန်ပေးပြီး map() ကမှ string[] ဖြစ်အောင် ပြောင်းပါတယ်။ map() ကို ဖြုတ်လိုက်ရင် : string[] annotation ကြောင့် compile error ရပါမယ်။

  4. Format for displayconsole.log(kmToMiles(10).toFixed(2));

    တွက်ချက်မှုရလဒ်က 6.21371 ပါ။ toFixed(2) က ဒသမ နှစ်နေရာနဲ့ string ပြန်ပေးလို့ 6.21 ပေါ်ပါတယ်။

You should see
6.21
[ 'Inception', 'Coco' ]

Exercise တွေကို ဘယ်လို လုပ်မလဲ

Starter ကို file တစ်ခုထဲ ကူးပါ

Challenge ထဲက starter code ကို practice-1.ts ထဲ ကူးပြီး npx tsc --strict --noEmit practice-1.ts နဲ့ type check လုပ်၊ npx tsx practice-1.ts နဲ့ run ပါ။

တစ်ခါ exercise တစ်ခု

Exercise တစ်ခုပြီးတိုင်း run ပါ။ သုံးခုလုံးရေးပြီးမှ run ရင် error တွေ ရောနေလို့ ဘယ်ကလာလဲ ခွဲရခက်ပါတယ်။

Edge case တွေနဲ့ စစ်ပါ

Celsius မှာ 0 → 32၊ 100 → 212၊ -40 → -40 ကို စစ်ပါ။ Book မှာ array အလွတ်နဲ့ book အားလုံး မရနိုင်တဲ့ case ကို စမ်းပါ၊ [] ပြန်ရပါမယ်။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

ဒီ exercise တွေက ကစားစရာလို ထင်ရပေမယ့် shape တွေက app အစစ်တွေထဲမှာ အတိအကျ ပေါ်ပါတယ်။ Weather app တစ်ခုက API ကနေ Celsius ရပြီး user setting အရ Fahrenheit ပြောင်းပြပါတယ်။ Online bookshop တစ်ခုရဲ့ "In stock only" checkbox က products.filter((p) => p.inStock) ပဲ ဖြစ်ပြီး React မှာ list ပြတဲ့အခါ .map() နဲ့ element တွေအဖြစ် ပြောင်းပါတယ်။

Team project တွေမှာ module အပြင်ကို export လုပ်တဲ့ function တွေမှာ return type ကို ရေးခိုင်းတဲ့ lint rule (ဥပမာ typescript-eslint ရဲ့ explicit-module-boundary-types) သုံးလေ့ရှိပါတယ်။ ဒီ lesson ရဲ့ problem section က အကြောင်းရင်းပါ။ Function ထဲက ပြောင်းလဲမှုတစ်ခုက တခြား file တွေထဲကို မမြင်ရဘဲ ပျံ့မသွားအောင် ကာကွယ်ပေးလို့ပါ။ System တစ်ခုရဲ့ အပိုင်းတစ်ခုက unit တစ်မျိုးကို ယူဆပြီး နောက်အပိုင်းက unit တခြားတစ်မျိုးကို ယူဆတဲ့အခါ unit conversion က ကုန်ကျစရိတ်ကြီးတဲ့ bug တွေရဲ့ ရင်းမြစ်ဖြစ်တတ်ပါတယ်။ ဒါကြောင့် team တွေက boundary တစ်နေရာတည်းမှာပဲ ပြောင်းပြီး value တွေကို temp အစား tempCelsius လို ရှင်းရှင်း နာမည်ပေးပါတယ်။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

filter() တစ်ခုတည်းနဲ့ title တွေ ရမယ်ထင်တာ

filter() က object တွေကို ရွေးပေးရုံပဲ လုပ်ပါတယ်၊ ပုံစံ မပြောင်းပါဘူး။ Title string တွေ လိုရင် နောက်က .map((b) => b.title) ကို ဆက်ရေးရပါမယ်။ Return type ကို string[] လို့ ရေးထားရင် ဒီအမှားကို compiler က ချက်ချင်း ဖမ်းပါတယ်။

Formula ကို မှားစီတာ

(celsius + 32) * 9 / 5 လို့ ရေးရင်လည်း number ပဲ ထွက်လို့ compile ဖြစ်ပါတယ်။ Type က formula ကို မစစ်ပေးပါဘူး။ 100 → 212 နဲ့ -40 → -40 လို အဖြေသိပြီးသား value နှစ်ခုနဲ့ အမြဲ စစ်ပါ။

Placeholder ကို ချန်ခဲ့တာ

Starter ထဲက return 0; နဲ့ return []; တွေက compile ဖြစ်အောင် ထည့်ထားတာပါ။ ဖြုတ်ဖို့ မေ့ရင် error မပေါ်ဘဲ 0 နဲ့ [] ပဲ ထွက်နေပါမယ်။ Output ကို အမြဲ ကြည့်ပါ။

Mini challenge

Practice Set 1: Book exercises

Warm-up ကို မကြည့်ဘဲ TODO လေးခုကို ဖြည့်ပါ။ Exercise 1 က temperature ပြောင်းတာ၊ 2 က Book interface နဲ့ data၊ 3 က ရနိုင်တဲ့ book title တွေ၊ 4 က author အလိုက် ရှာတာပါ။

  • celsiusToFahrenheit(100) က 212၊ (-40) က -40 ပြန်ပေးရပါမယ်
  • Book object သုံးခုမှာ field လေးခုလုံး ပါရပါမယ်
  • getAvailableBooks() က filter() နဲ့ map() ကို ချိတ်သုံးပြီး string[] ပြန်ပေးရပါမယ်
  • getBooksByAuthor() က Book[] ကို ပြန်ပေးရပါမယ်
practice-1.ts
// Exercise 1
function celsiusToFahrenheit(celsius: number): number {
  // TODO: (celsius * 9/5) + 32
  return 0;
}

// Exercise 2
interface Book {
  title: string;
  author: string;
  year: number;
  isAvailable: boolean;
}

const books: Book[] = [
  // TODO: add 3 book objects (at least one with isAvailable: false)
];

// Exercise 3
function getAvailableBooks(list: Book[]): string[] {
  // TODO: filter() the available books, then map() them to titles
  return [];
}

// Exercise 4
function getBooksByAuthor(list: Book[], author: string): Book[] {
  // TODO: keep only the books by this author
  return [];
}

ရလဒ်: 212၊ -40၊ ရနိုင်တဲ့ title နှစ်ခုပါတဲ့ array နဲ့ Ko Min ရဲ့ book ရဲ့ year [ 2021 ] ကို ပြပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

Exercise 3 မှာ filter() ပြီးရင် type က Book[] ပဲ ရှိသေးတယ်ဆိုတာ သတိရပါ။ Exercise 4 မှာတော့ map() မလိုပါဘူး၊ Book object တွေကို တိုက်ရိုက် ပြန်ပေးရမှာပါ။

အဖြေ ကြည့်ရန်
practice-1.ts
// Exercise 1
function celsiusToFahrenheit(celsius: number): number {
  return (celsius * 9) / 5 + 32;
}

// Exercise 2
interface Book {
  title: string;
  author: string;
  year: number;
  isAvailable: boolean;
}

const books: Book[] = [
  { title: "TypeScript Basics", author: "Aye Aye", year: 2023, isAvailable: true },
  { title: "Node in Practice", author: "Ko Min", year: 2021, isAvailable: false },
  { title: "Clean Functions", author: "Aye Aye", year: 2019, isAvailable: true },
];

// Exercise 3
function getAvailableBooks(list: Book[]): string[] {
  return list.filter((b) => b.isAvailable).map((b) => b.title);
}

// Exercise 4
function getBooksByAuthor(list: Book[], author: string): Book[] {
  return list.filter((b) => b.author === author);
}

console.log(celsiusToFahrenheit(100));
console.log(celsiusToFahrenheit(-40));
console.log(getAvailableBooks(books));
console.log(getBooksByAuthor(books, "Ko Min").map((b) => b.year));

Quick Quiz 1 / 3

books: Book[] ဖြစ်ရင် books.filter((b) => b.isAvailable) ရဲ့ type က ဘာလဲ။

Quick Quiz 2 / 3

celsiusToFahrenheit(-40) က ဘာပြန်ပေးမလဲ။

Quick Quiz 3 / 3

Strict mode မှာ function getTitles(books) { return books.map((b) => b.title); } ရဲ့ ဘယ်အပိုင်းကို compiler က ငြင်းမလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Function signature ကို spec အဖြစ် အရင်ဖတ်ပြီး return type ကို ရေးထားရင် bug က မှားတဲ့ line မှာပဲ ပေါ်ပါတယ်
  • filter() က ရွေးပြီး type မပြောင်းပါ၊ map() က ပုံစံပြောင်းပါတယ်
  • Compile ဖြစ်တာနဲ့ logic မှန်တာ မတူလို့ 100 → 212၊ -40 → -40 လို သိပြီးသား value တွေနဲ့ run ပါ
  • Typed array literal က object တိုင်းကို interface နဲ့ တိုက်စစ်ပေးပါတယ်

TypeScript Handbook — TypeScript

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Practice Exercises - Set 1 (Basics) | Thuta Learning