# TS - Generic

### Generic ဆိုတာ ?

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FJXaXJ7HBHbJmToHaCpyv%2FTypescript%20Generic%20Type%20Syntax.png?alt=media&#x26;token=cd4aa758-c506-46be-8a1c-08fff1c285e3" alt=""><figcaption></figcaption></figure>

Generic ဆိုတာ အလွယ်အားဖြင့် မသိသေးတဲ့ data type ကို လက်ခံရရှိပြီး အဲ့ data type ကို ပြန်လည်အသုံးချတာဖြစ်ပါတယ် ။ Syntax က `<` နဲ့ `>` ကြားထဲမှာ Generic Type ကိုထည့်သွင်း အသုံးပြုတာဖြစ်ပါတယ်။

### Generic Function

`<T> ( value: T ): T` ရေးသားနည်းနဲ့ Generic Function ကို ရေးသားပါတယ် ။&#x20;

T က Dynamic Type ကို ရည်ညွှန်းထားတာပါ ။&#x20;

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2Fkdfzn2uBuDLH960XFg5Y%2FTypescript%20Generic%20Function.png?alt=media&#x26;token=37a6c2fa-9745-4bfb-8a82-23df7a8a5f43" alt=""><figcaption></figcaption></figure>

`T` ထဲကို အခြား Type တွေကို အစားထိုး ကြည့်မယ်ဆိုရင် ...

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FKqDN77V3geTsLwXpBFWN%2FTypescript%20Generic%20Function%20Setting%20Value.png?alt=media&#x26;token=c9cec6df-b5f2-4681-96fc-3875c960c666" alt=""><figcaption></figcaption></figure>

`T` က string အနေနဲ့ အစားထိုးမယ်ဆိုရင် ၊ လက်ခံမယ့် Parameter `(value: string)` ဖြစ်ပြီး Return type က လည်း `string` ဖြစ်သွားမှာပါ ။

`T` က number ဖြစ်မယ်ဆိုရင် ၊ Parameter `(value: number)` ဖြစ်ပြီး Return type က `number` ဖြစ်သွားမှာပါ ။&#x20;

\
`T = boolean` ဆိုလည်း ထိုနည်းလည်းကောင်းပါပဲ ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FjH5T7lOgE6PNIcccEj9s%2FTypescript%20Generic%20Function%20Setting%20Value%20Code.png?alt=media&#x26;token=7603935c-c11d-463b-89fc-2657bda05743" alt=""><figcaption></figcaption></figure>

ဘယ်ဘက် code အရ `identity("Hello World")` function ခေါ်လိုက်တာက ၊ Parameter အရ `string` type ဖြစ်တဲ့အတွက် Return Type သည် `string` type ပဲ ရလာမှာဖြစ်ပါတယ် ။ ဒါကို `str` variable ကို mouse နဲ့ ခဏအကြာ hover လုပ်လိုက်ရင် `let str: string` ဆိုပြီး  ပေါ်လာတာတွေ့နိုင်မှာပါ ။

ညာဘက်ပုံ code  `identity(42)` မှာ ၊ Parameter `value` type ဖြစ်ပြီး Return ကလည်း `value` type ဖြစ်လာပါတယ် ။

&#x20;ဒီနည်းလမ်းနဲ့ Generic Type \<T> Function တွေ ရေးသားနိုင်ပါတယ်  ။

အခု Primitive Type တွေ အစား Complex ဖြစ်တဲ့ User Object တစ်ခုလုပ်ပြီး ထည့်သွင်း အသုံးပြုကြည့်ပါမယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FG1xqtyYri3BdeoqKpK30%2FTypescript%20Generic%20Function%20User%20Type.png?alt=media&#x26;token=b79f5221-eba3-4a7f-a02f-e4851ec89756" alt=""><figcaption></figcaption></figure>

User Object type တစ်ခု ဖန်တီးလိုက်ပါမယ် ။&#x20;

စာဖတ်သူတို့က `< T >`  နေရာမှာ User Type ကို အစားထိုးကြည့်လိုက်ပါ ။

Return Type ကလည်း User Type ရသွားမှာဖြစ်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2F2pSTzgq825uR58LTKTyb%2FTypescript%20Generic%20Function%20User%20Type%20Code.png?alt=media&#x26;token=75878868-0f78-4b7a-b09a-3c2743547104" alt=""><figcaption></figcaption></figure>

Code အပြည့်စုံကို မိမိစက်ထဲမှာ ထည့်သွင်း စမ်းရေးကြည့်ပါ ။

### Exercise for Generic

Exercise အနေနဲ့  စာဖတ်သူတို့ကို ပုစ္ဆာတစ်ပုဒ် စဉ်းစားခိုင်းပါမယ် ။

<mark style="color:yellow;">successResponse</mark><mark style="color:red;">(</mark><mark style="color:orange;">"Hello"</mark><mark style="color:red;">)</mark> function ကို ခေါ်သုံးလိုက်လျင် ၊ Return အနေနဲ့&#x20;

<mark style="color:purple;">{</mark> <mark style="color:blue;">success</mark>: <mark style="color:green;">true</mark>, <mark style="color:blue;">data</mark>: <mark style="color:yellow;">"Hello"</mark> <mark style="color:purple;">}</mark>ကို ပြန်ရမှာဖြစ်ပါတယ် ။

<mark style="color:yellow;">successResponse</mark><mark style="color:red;">(</mark> <mark style="color:purple;">{</mark> <mark style="color:purple;">id</mark>: <mark style="color:green;">1</mark>, <mark style="color:purple;">name</mark>: <mark style="color:yellow;">"LMP"</mark> <mark style="color:purple;">}</mark> <mark style="color:red;">)</mark>  function မှာ ၊ Return Type က&#x20;

<mark style="color:red;">{</mark> <mark style="color:blue;">success</mark>: <mark style="color:green;">true</mark>, <mark style="color:blue;">data</mark>: <mark style="color:purple;">{</mark>  <mark style="color:purple;">id</mark>: <mark style="color:green;">1</mark>, <mark style="color:purple;">name</mark>: <mark style="color:yellow;">"LMP"</mark> <mark style="color:purple;">}</mark> <mark style="color:red;">}</mark> ကို ပြန်ရမှာဖြစ်ပါတယ် ။

စာဖတ်သူတို့က <mark style="color:yellow;">`successResponse`</mark> ကို generic function တစ်ခုအနေနဲ့ ရေးကြည့်ပါ ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FJQVEmWVjY4g8h8mq5mjj%2FTypescript%20Generic%20Question.png?alt=media&#x26;token=65eb1eec-f7cd-4442-b8b0-7f25f23d11e2" alt=""><figcaption></figcaption></figure>

ဟုတ်ပါတယ် Return type နေရာမှာ `{ success: boolean, data: T }` လို့ပြောင်းပေးလိုက်ရင် မှန်ပါတယ် ။  `T`  နေရာမှာ အစားထိုးလိုက်တဲ့ Code ပုံလေးကို တစ်ချက်ကြည့်ကြပါမယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FG9sq9jSzhvpIUDFnqal6%2FTypescript%20Generic%20Function%20Setting%20Success%20Value.png?alt=media&#x26;token=cda05222-cd72-4b4b-9287-98146e5d661c" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FvIJNnu1dEGItNrPIqsWX%2FTypescript%20Generic%20Function%20Setting%20Success%20Value%20Code.png?alt=media&#x26;token=0d9e4131-bbc8-4bfe-b6e6-615c2d986df0" alt=""><figcaption></figcaption></figure>

helloResult ကို mouse နဲ့ ခဏအကြာ တင်ထားလိုက်ရင် ၊&#x20;

return type ကို ထည့်လိုက်တဲ့ Type အတိုင်း Dynamic Return ပြန်နေတာတွေ့ရမှာပါ ။

### Generic Type

`type Generic<T> = T`  ရေးသားနည်းနဲ့ Generic Type ကို ရေးသားနိုင်ပါတယ် ။

Exercise မှာရေးခဲ့တဲ့ Return Type ကိုပဲ Custom Generic Type အနေနဲ့ ရေးပြသွားပါမယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FcjgJG6FIpuVhf1twqEGM%2FTypescript%20Generic%20Z%20Create%20Response%20Type.png?alt=media&#x26;token=54a43b64-50aa-4064-9e57-c6071b408d50" alt=""><figcaption></figcaption></figure>

Type ကို Response နာမည်နဲ့ Generic Type အမျိုးအစားရေးသားလိုက်ပါတယ် ။&#x20;

ညာဘက် Code က string type ကို အစားထိုး ရေးထားတာဖြစ်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2F87C4Jpt3X2OTeqp4Pq4H%2FTypescript%20Generic%20Z%20Create%20Response%20Type%20Code.png?alt=media&#x26;token=6bc753d1-0c7c-4157-9a3f-98eb239c2bbd" alt=""><figcaption></figcaption></figure>

နမူနာ Code ထဲကအတိုင်း Response ဆိုရင် data နေရာမှာ number type ဖြစ်မှာပါ ။

`<` နဲ့ `>` ကြားထဲမှာ Type တွေကို ပြောင်းလဲ ပြီး မိမိစက်ထဲမှာ စမ်းနိုင်ပါတယ် ။

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FrFBaDRa7zL5pkll7W4ho%2FTypescript%20Generic%20Z%20Create%20Response%20Type%20With%20SuccessRepsponse%20Code.png?alt=media&#x26;token=8a2016e7-224c-406a-9a2f-e7b59d110592" alt=""><figcaption></figcaption></figure>

နားလည်သွားပြီဆိုရင် ခုဏက Exercise ပုစ္ဆာရဲ့ Response Type ကို အခုလို ပြောင်းလဲ ရေးသားလိုက်လို့ရပါပြီ ။
