Thuta Learning
Angular
BasicWeb Developmentအလယ်အလတ်

Intro to TypeScript

Angularသင်ခန်းစာ 420 မိနစ်

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

  • Intro to TypeScript ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

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

Intro to TypeScript ကို ရိုးရိုးလေးနားလည်မယ်

Angular မှာ TypeScript ကိုအဓိကအသုံးပြုပါတယ်။ TypeScript က JavaScript ပေါ်မှာ type system, interface, class structure စတာတွေ ထပ်ပေါင်းထားတဲ့ language ဖြစ်လို့ project ကြီးလာတဲ့အခါ mistake တွေကို စောစောဖမ်းနိုင်ပါတယ်။ အထူးသဖြင့် API data, form value, component input/output တွေမှာ type ထည့်ရေးခြင်းက code ကိုပိုယုံကြည်ရစေပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

`any` သုံးပြီး compiler error ပျောက်အောင်လုပ်ခြင်းက error ကို runtime ဆီရွှေ့တာသာဖြစ်ပါတယ်။

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

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Angular code ရဲ့ contract များကို TypeScript interfaces, unions, generics နဲ့ strict null checks က compile time မှာကူညီစစ်ပေးပါတယ်။ Type annotation က runtime validation မဟုတ်သဖြင့် API/user input ကဲ့သို့ external data ကိုသီးခြား validate လုပ်ရပါတယ်။ `any` ကိုရှောင်ပြီး `unknown` ကို narrow လုပ်ခြင်း၊ readonly data နဲ့ explicit return types သုံးခြင်းက refactoring ကိုပိုလုံခြုံစေပါတယ်။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Intro to TypeScript ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// TypeScript allows us to add types to our variables.
let name: string = "Angular";
let version: number = 16;
let isAwesome: boolean = true;

function greet(person: string): string {
    return "Hello, " + person;
}

Angular code ရဲ့ contract များကို TypeScript interfaces, unions, generics နဲ့ strict null checks က compile time မှာကူညီစစ်ပေးပါတယ်။ Type annotation က runtime validation မဟုတ်သဖြင့် API/user input ကဲ့သို့ external data ကိုသီးခြား validate လုပ်ရပါတယ်။ `any` ကိုရှောင်ပြီး `unknown` ကို narrow လုပ်ခြင်း၊ readonly data နဲ့ explicit return types သုံးခြင်းက refactoring ကိုပိုလုံခြုံစေပါတယ်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input + state// TypeScript allows us to add types to our variables.

    Angular မှာ TypeScript ကိုအဓိကအသုံးပြုပါတယ်။ TypeScript က JavaScript ပေါ်မှာ type system, interface, class structure စတာတွေ ထပ်ပေါင်းထားတဲ့ language ဖြစ်လို့ project ကြီးလာတဲ့အခါ mistake တွေကို စောစောဖမ်းနိုင်ပါတယ်။ အထူးသဖြင့် API data, form value, component input/output တွေမှာ type ထည့်ရေးခြင်းက code ကိုပိုယုံကြည်ရစေပါတယ်။

  2. Component + templatelet isAwesome: boolean = true;

    Angular code ရဲ့ contract များကို TypeScript interfaces, unions, generics နဲ့ strict null checks က compile time မှာကူညီစစ်ပေးပါတယ်။ Type annotation က runtime validation မဟုတ်သဖြင့် API/user input ကဲ့သို့ external data ကိုသီးခြား validate လုပ်ရပါတယ်။ `any` ကိုရှောင်ပြီး `unknown` ကို narrow လုပ်ခြင်း၊ readonly data နဲ့ explicit return types သုံးခြင်းက refactoring ကိုပိုလုံခြုံစေပါတယ်။

  3. Rendered Angular UI}

    User model နဲ့ loading/success/error union state တစ်ခုရေးပြီး invalid property access ကို compiler ကဖမ်းကြောင်းစမ်းပါ။ JSON response တစ်ခုကို runtime guard ဖြင့်စစ်ပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Success

Intro to TypeScript ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

`any` သုံးပြီး compiler error ပျောက်အောင်လုပ်ခြင်းက error ကို runtime ဆီရွှေ့တာသာဖြစ်ပါတယ်။

Angular မှာ TypeScript ကိုအဓိကအသုံးပြုပါတယ်။ TypeScript က JavaScript ပေါ်မှာ type system, interface, class structure စတာတွေ ထပ်ပေါင်းထားတဲ့ language ဖြစ်လို့ project ကြီးလာတဲ့အခါ mistake တွေကို စောစောဖမ်းနိုင်ပါတယ်။ အထူးသဖြင့် API data, form value, component input/output တွေမှာ type ထည့်ရေးခြင်းက code ကိုပိုယုံကြည်ရစေပါတယ်။

typescript
// TypeScript allows us to add types to our variables.
let name: string = "Angular";
let version: number = 16;
let isAwesome: boolean = true;

function greet(person: string): string {
    return "Hello, " + person;
}
You should see
(TypeScript သည် compile time တွင် error များကိုစစ်ဆေးပေးပြီး JavaScript သို့ compile လုပ်သည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

User model နဲ့ loading/success/error union state တစ်ခုရေးပြီး invalid property access ကို compiler ကဖမ်းကြောင်းစမ်းပါ။ JSON response တစ်ခုကို runtime guard ဖြင့်စစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Intro to TypeScript နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// TypeScript allows us to add types to our variables.
let name: string = "Angular";
let version: number = 16;
let isAwesome: boolean = true;

function greet(person: string): string {
    return "Hello, " + person;
}

ရလဒ်: Intro to TypeScript ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

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

User model နဲ့ loading/success/error union state တစ်ခုရေးပြီး invalid property access ကို compiler ကဖမ်းကြောင်းစမ်းပါ။ JSON response တစ်ခုကို runtime guard ဖြင့်စစ်ပါ။

Quick Quiz 1 / 3

Intro to TypeScript ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Input + state → Component + template → Rendered Angular UI
  • Angular code ရဲ့ contract များကို TypeScript interfaces, unions, generics နဲ့ strict null checks က compile time မှာကူညီစစ်ပေးပါတယ်။ Type annotation က runtime validation မဟုတ်သဖြင့် API/user input ကဲ့သို့ external data ကိုသီးခြား validate လုပ်ရပါတယ်။ `any` ကိုရှောင်ပြီး `unknown` ကို narrow လုပ်ခြင်း၊ readonly data နဲ့ explicit return types သုံးခြင်းက refactoring ကိုပိုလုံခြုံစေပါတယ်။
  • `any` သုံးပြီး compiler error ပျောက်အောင်လုပ်ခြင်းက error ကို runtime ဆီရွှေ့တာသာဖြစ်ပါတယ်။
  • User model နဲ့ loading/success/error union state တစ်ခုရေးပြီး invalid property access ကို compiler ကဖမ်းကြောင်းစမ်းပါ။ JSON response တစ်ခုကို runtime guard ဖြင့်စစ်ပါ။

TypeScript — Handbook — Microsoft

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `any` သုံးပြီး compiler error ပျောက်အောင်လုပ်ခြင်းက error ကို runtime ဆီရွှေ့တာသာဖြစ်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

Intro to TypeScript နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (TypeScript သည် compile time တွင် error များကိုစစ်ဆေးပေးပြီး JavaScript သို့ compile လုပ်သည်)

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

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

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

Intro to TypeScript | Thuta Learning