> 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-typeof.md).

# TS - typeof

### typeof&#x20;

`typeof` ကို JavaScript runtime မှာ သုံးမယ်ဆိုရင် variable ရဲ့ type ကို `string` အနေနဲ့ return ပြန်ပါမယ်။

TypeScript ရေးသားနည်းမှာ `typeof` ကို သုံးမယ်ဆိုရင်တော့ variable ရဲ့ Type Structure ကို ပြန်ရမှာဖြစ်ပါတယ် ။

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

အခု နမူနာ Code ထဲကအတိုင်း `typeof user` က JavaScript Runtime မှာဆို "object" ဆိုတဲ့ String ကိုရသွားမှာပါ ။ &#x20;

TypeScript ရေးသားနည်းမှာ  `typeof variable` ပုံစံနဲ့  Object Structure ကို `CustomType` အဖြစ် ပြုလုပ်နိုင်ပါတယ် ။

`appConfig` Code ကို လေ့လာသွားကြပါမယ် ။&#x20;

ပထမဆုံး `appName` နဲ့ `env` property တွေပါတဲ့ appConfig ဆိုတဲ့ constant variable တည်ဆောက်ထားပါတယ် ။

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

Custom Type တစ်ခုအဖြစ် `ConfigType` ကို constant appConfig က တဆင့် `typeof appConfig` သုံးပြီး ပြုလုပ်လိုက်ပါတယ် ။

အခြား `devConfig` နဲ့ `prodConfig`  Config Variables တွေအတွက်  Reusable Type အဖြစ် အသုံးပြုနိုင်သွားပါပြီ ။

Type တွေကို ကိုယ်တိုင် သတ်မှတ်တာမဟုတ်ဘဲ Variable ဆီက Type ကို ပုံတူပွားတာမျိုး ဖြစ်တဲ့အတွက် အသုံးများပါတယ်။

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

Custom Type မတည်ဆောက်တော့ပဲ တိုက်ရိုက် `variable: typeof otherVariable`  အခုလိုပုံစံမျိုးနဲ့လည်း ရေးသားတာမျိုး ရှိပါတယ် ။ ဒါက ရိုးရှင်းတဲ့ Type Alias တစ်မျိုးပဲဖြစ်ပါတယ် ။

### keyof typeof တွဲရေးခြင်း

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

ဒါကလည်း အသုံးများတဲ့ ပုံစံလေးဖြစ်ပါတယ် ။&#x20;

`typeof` ကို သုံးလိုက်တဲ့အခါ variable ရဲ့ Type Structure ကိုရသွားမှာပါ ။&#x20;

ပြီးတော့မှ သူ့ရဲ့ အရှေ့မှာ keyof နဲ့ union keys တွေကို ထုတ်ယူလိုက်တာပဲဖြစ်ပါတယ် ။
