> For the complete documentation index, see [llms.txt](https://lwin-moe-paing.gitbook.io/typescript-baby-by-lwin-moe-paing/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lwin-moe-paing.gitbook.io/typescript-baby-by-lwin-moe-paing/basic-typescript/typescript-annotation-vs-inference.md).

# TypeScript Annotation vs Inference

Annotation vs Inference ကို အလွယ်ရှင်းပြရရင်&#x20;

* Type Annotation 👉 Type ကို manually သတ်မှတ် ခြင်း
* Type Inference 👉 Type ကို TypeScript က auto ယူဆခြင်း&#x20;

တို့ပဲ ဖြစ်ပါတယ် ။

### TypeScript Basic Syntax

<figure><img src="/files/BkWbvnNV2dk7D1zWkmGI" alt=""><figcaption></figcaption></figure>

အခြေခံအရ ( : ) column ကိုအသုံးပြုပြီး အနောက်မှာ type ကိုသတ်မှတ်ပါတယ် ။&#x20;

### TypeScript Annotation

Type ကို manually သတ်မှတ်ခြင်း code ကို လေ့လာကြည့်ကြပါမယ် ။

```typescript
let myName: string = "LMP";

let age: number = 28;

let isStudent: boolean = true;
```

Programmer က Type ကို ကိုယ်တိုင်စီမံရေးသားတာ ဖြစ်ပါတယ် `myName` variable ထဲကို <mark style="color:red;">string</mark> Type ပဲထည့်ပါမယ်လို့ ကြေငြာထားတာဖြစ်ပါတယ် ။ `age` ထဲကို <mark style="color:red;">number</mark> Type ဖြစ်ပြီး ၊ `isStudent` ထဲမှာ <mark style="color:red;">boolean</mark> Type အဖြစ်နဲ့ သုံးထားပါတယ် ။

Type Annotation က `variable`, `function parameter`, `return type`, `class property` တွေမှာ သတ်မှတ်နိုင်ပါတယ် ။ နောက်ပိုင်းအခန်းတွေမှာ ဆက်ပြီး လေ့လာ အသုံးပြုသွားမှာပါ ။

### TypeScript Inference

&#x20;TypeScript က သင့်တော်တဲ့ Type ကို အလိုအလျှောက် အသုံးပြုပေးတာကို Inference လို့ခေါ်ပါတယ် ။

```typescript
let age = 25;  // `age` ကို number type လို့ သတ်မှတ်မယ်။

let name = "John";  // `name` ကို string type လို့ သတ်မှတ်မယ်။
```

Type ကို ကိုယ်တိုင် (Manual) သတ်မှတ် ပေးစရာမလိုပါဘူး ။ `age` variable ထဲကို တန်ဖိုး 25 ထည့်လိုက်တဲ့အခါ TypeScript က အလိုအလောက် <mark style="color:red;">number</mark> type ဖြစ်သတ်လို့ သတ်မှတ်ပေးလိုက်ပါတယ် ။

<figure><img src="/files/USergaVxIcKKPvaWS7MV" alt=""><figcaption></figcaption></figure>

စာဖတ်သူက `age` variable ကို Mouse နဲ့ ခဏကြာကြာလေး ငြိမ်ထားရင် သူ့ရဲ့ Type ကို မြင်နိုင်ပါတယ် ။

နောက်တစ်လိုင်းမှာ `age = 'string';`   string  တန်ဖိုးထည့်လိုက်ရင် TypeScript server ကနေ Error Feedback ပြန်ပေးပါလိမ့်မယ် ။

<figure><img src="/files/WpkZcIZKJ8QumpfBAkqQ" alt=""><figcaption></figcaption></figure>

ဒါဆို TypeScript ဘယ်လိုအလုပ်လုပ်သလဲ Inference နဲ့ Annotation ဆိုတာဘာလဲ အခြေခံအားဖြင့်  သိသွားပါပြီ ။

### Inference နဲ့ Annotation ကို ဘယ်အချိန်မှာ ဘယ်ဟာသုံးရမှာလဲ ?

ပုံမှန်အားဖြင့် Primitive Data Type တွေကိုသုံးတဲ့အခါ Inference (အလိုအလျောက် သတ်မှတ်ပေးခြင်း) ကိုသုံးလေ့ရှိပါတယ် ။

Primitive Types (7 မျိုး) - `string`, `number`, `boolean`, `bigint`, `symbol`, `null`, `undefined` တို့ဖြစ်ပါတယ် ။

အခြား  Complex ဖြစ်တဲ့ Object Type တွေကိုတော့ Annotation သုံးလေ့ရှိကြပါတယ် <br>
