> 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/ts-function.md).

# TS - Function

### Function Type

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FvXyQs8Tl8Qc05iSXjWSM%2FTypescript%20Function%20Syntax.png?alt=media&amp;token=141a5bfe-20f7-4953-bb56-c43ac99dcdcf" alt=""><figcaption></figcaption></figure>

function type တစ်ခုကို ပြုလုပ်မယ်ဆိုရင် Parameter type ရယ် ၊ Return type ရယ် သတ်မှတ်ပေးဖို့လိုပါတယ် ။ Parameter မပါဘူးဆိုရင် `()` empty parameter အနေနဲ့  ရေးသားလို့လည်းရပါတယ် ။ အရင်ဆုံး `addFn` ဆိုတဲ့ ဂဏန်း (၂) လုံးပေါင်းတဲ့ function တစ်ခု တည်ဆောက်ပါမယ် ။

### Arrow Function

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2Fw6FlJHpBESBId61n2XW8%2FTypescript%20Function%20Arrow%20Type.png?alt=media&amp;token=3272edac-74ad-4e0c-a692-5d89908d9a7f" alt=""><figcaption></figcaption></figure>

`addFn` arrow function အဖြစ် ရေးသာမှာဖြစ်ပါတယ် ။&#x20;

Parameters အနေနဲ့ `a` ရယ် `b` ရယ် အတွက် number type တွေ လက်ခံထားပါတယ် ။&#x20;

ပြီးတဲ့နောက် `:` နောက်မှာ Return Type အဖြစ် Number Type ပဲ ပြန်မယ်လို့ ရေးသားလိုက်ပါတယ် ။&#x20;

အခု ဆို Function တစ်ခု ဘယ်လို ဖန်တီး ရမလဲဆိုတာ လေ့လာခဲ့ပြီး ပါပြီ ။&#x20;

ဒါဆို Manual သတ်မှတ်ထားတဲ့ Return Type ကို ဖြုတ်ကြည့်လိုက်ပါမယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FFZ6Ap7TlV7iOTdcL6xd1%2FTypescript%20Function%20Arrow%20Type%20Without%20Return%20Type.png?alt=media&amp;token=f77eee8c-782b-4f6e-bfde-f80b4ee3ea03" alt=""><figcaption></figcaption></figure>

Return Type ကို Inference ပုံစံ TypeScript က အလိုအလျောက် သတ်မှတ်ပေးသွားမှာပါ ။

### Normal Function

Arrow Function ရေးသားနည်း လေ့လာပြီးသွားတဲ့အခါ ၊ ရိုးရိုး `function` keyword နဲ့ ရေးသားထားတာကို ကြည့်ကြရအောင် ... ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2F3BDyX911lJYSdSp98Tii%2FTypescript%20Function%20Normal%20Type.png?alt=media&amp;token=c216e13e-f759-46bf-b832-c051f9b4411b" alt=""><figcaption></figcaption></figure>

`=` နဲ့ Arrow `=>` မပါတာက လွဲရင် ကျန်တဲ့ပုံစံကတော့ အတူတူပါပဲ ။&#x20;

အနောက်မှာလည်း Return Type ကို Manual သတ်မှတ်ပေးနိုင်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2Fu7Dq8iMiYCq9xF2taMIO%2FTypescript%20Function%20Normal%20Type%20Without%20Return%20Type.png?alt=media&amp;token=28c45ab2-eed5-4ef4-93fc-e32bd844a4cb" alt=""><figcaption></figcaption></figure>

Return Type ကို ဖြုတ်ကြည့်ပြီး Inference လုပ်သွားတာကိုလည်း အခုလို ရေးသားနိုင်ပါတယ် ။

### Custom Type Function

`type` keyword နဲ့ Custom Function Type ရေးသားနည်းကို လေ့လာသွားကြပါမယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2F6EF3iAexYeLHxPqMwVb8%2FTypescript%20Function%20Custom%20Type.png?alt=media&amp;token=b4602761-d857-4b59-9ba8-bb3b0fe45a01" alt=""><figcaption></figcaption></figure>

ထူးခြားချက်အနေနဲ့ `:` မဟုတ်တော့ပဲ `=>` arrow sign နဲ့ Return Type  သတ်မှတ်ပေးတာ သတိပြုရမှာပါ ။ ကျန်တာကတော့ အတူတူပဲဖြစ်ပါတယ် ။  `AddFn` Type ကို variable  ကယူသုံးတဲ့အခါ parameter `a` နဲ့ `b` ကို type တွေ ထပ်သတ်မှတ်ပေးစရာမလိုတော့ပါဘူး ။ `AddFn` Type ထဲမှာတင် `a` နဲ့ `b` က number type ဆိုတာကို  TypeScript က ကြိုတင်သိနေပြီးသားမို့လို့ ဖြစ်ပါတယ် ။

### Function Void Type

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2F3drIza6bTPfi3MyvGkEg%2FTypescript%20Function%20Void%20Type.png?alt=media&amp;token=ac275efb-d800-421c-ac2e-d5a04be20a13" alt=""><figcaption></figcaption></figure>

Return တန်ဖိုးမရှိတဲ့ function တွေအတွက် void တန်ဖိုး ထည့်သွင်း အသုံးပြုနိုင်ပါတယ် ။

### Function Overloading

Function Overloading ဆိုတာက function တစ်ခုပဲ သုံးပြီး အမျိုးမျိုးသော Parameter type တွေ ၊ Return type တွေကို သီးခြားသတ်မှတ်ဖို့ ဖြစ်ပါတယ် ။

* Different Input Types ကို Handle လုပ်နိုင်ဖို့&#x20;
* Code Readability & Maintainability တိုးစေဖို့&#x20;
* Function name တစ်ခုပဲ သုံးပြီး အမျိုးမျိုးသော လုပ်ဆောင်မှုများစွာ ဖန်တီးနိုင်ဖို့ ဖြစ်ပါတယ် ။

ဥပမာ အနေနဲ့ Parameters (၂) မျိုးလက်ခံမယ့် `add` function ကို စဉ်းစားကြည့်ပါမယ် ။ Idea က number နှစ်လုံးထည့်ရင် ပေါင်းပေးမယ် ၊ string နှစ်လုံးထည့်ရင် string (၂) ခုကို concat လုပ်ပေးလိုက်မယ် ။&#x20;

Function Overloading ကို အသုံးပြုပြီး ရေးသားကြပါမယ် ။&#x20;

သူ့မှာ အပိုင်း (၂) ပိုင်းပါဝင်ပါတယ် ပထမအပိုင်းက **Signature** သတ်မှတ်ခြင်း ၊ ဒုတိယ အပိုင်းက **Code** **Implementation** လုပ်ခြင်းပဲ ဖြစ်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2F09EMbnipXJzeHP57rFo9%2FTypescript%20Function%20Overloading%20Question.png?alt=media&amp;token=9a697ddc-9465-4f65-b6cb-e9a800696322" alt=""><figcaption></figcaption></figure>

#### Signature သတ်မှတ်ခြင်း&#x20;

အရင်ဆုံး `function` keyword ကို သုံးပြီး ကိုယ်ဖြစ်စေချင်တဲ့ signature ကို ရေးသားရမှာဖြစ်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FntkFablc0G0rPefQtqof%2FTypescript%20Function%20Overloading%20Signature%20Types.png?alt=media&amp;token=f1b835ce-978a-4da2-8e40-55b67f61f55d" alt=""><figcaption></figcaption></figure>

1st Signature မှာ parameter `a`  number type ဖြစ်ပြီး parameter `b` လည်း number ဖြစ်ရင် number type ပဲ return ပြန်ပါမယ် ။

2nd Signature မှာ parameter `a`  string type ဖြစ်ပြီး parameter `b` လည်း string ဖြစ်ရင် string type ပဲ return ပြန်ပါမယ် ။

ပြီးတဲ့အခါ သတ်မှတ်ထားတဲ့ Signature ရဲ့ Parameter တွေကို Cover ဖြစ်အောင် Code Implementation ပြန်လုပ်ပေးရမှာပါ ။

#### Implementation ပြုလုပ်ခြင်း

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FO8D3BR7OhGGQ2KnukUXK%2FTypescript%20Function%20Overloading%20Implementation%20Types.png?alt=media&amp;token=eb835f49-b3de-497a-9cf0-704d931be8d0" alt=""><figcaption></figcaption></figure>

1st Signature မှာ `a` parameter သည် `number` type လက်ခံပြီး 2nd Signature အတွက် `string` type ကိုလက်ခံထားပါတယ် ။ ဒါကြောင့် Implementation Function သည် signature ၂ ခုလုံး cover ဖြစ်အောင် ပထမ Parameter ကို `number | string` နဲ့ လက်ခံရပါမယ် ။ parameter `b` အတွက်ကော return type အတွက်ပါ cover ဖြစ်အောင် string | number နဲ့ ပြောင်းလဲပြီး implement လုပ်ထားလိုက်ပါပြီ ။

ပြီးတဲ့အခါကြမှ JavaScript Code Implementation အဆင့်ရောက်ပြီး Logic ကို ရေးသားကြရမှာဖြစ်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FFTPaoin5rQOpv7cEHjQw%2FTypescript%20Function%20Overloading%20Code%20Simple.png?alt=media&amp;token=c90c2736-14c1-4155-9d0d-c30168902de1" alt=""><figcaption></figcaption></figure>

Code အပြည့်အစုံကို ရေးသားဖော်ပြလိုက်ပါတယ် ။ ဒီလောက်ဆို Function Overload ကို အခြေခံအားဖြင့် နားလည်သွားပြီလို့ ယူဆပါတယ် ။
