> 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-as-type-casting.md).

# TS - as Type Casting

<figure><img src="/files/6qjLKlmqDkQjLbP6hXWB" alt=""><figcaption></figcaption></figure>

TypeScript ရဲ့ `"as"` keyword ဟာ Type Assertion (Type Casting) လို့ခေါ်ပါတယ် ။

`someVariable as TargetType` ရေးသားနည်းနဲ့ ပြောင်းလဲစေချင်တဲ့ Type ကို Modify လုပ်နိုင်ပါတယ် ။

JSON parse နမူနာ Code ကို အရင်ကြည့်ရအောင် ... ။

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

JSON.parse method က any type ပဲ ပြန်ပေးပါတယ် ။&#x20;

ဒါကြောင့် responseData ထဲမှာ any ဖြစ်နေမှာပါ ။&#x20;

သို့ပေ့မယ် မိမိက User Type အနေနဲ့ ပြန်ရမယ်ဆိုတာ ကြိမ်းသေသိနေတဲ့ အခြေအနေမျိုးဖြစ်နေတဲ့အခါ ၊&#x20;

User Type အနေနဲ့ Type Casting လုပ်လိုက်တာ Type safe ပိုဖြစ်သွားမှာပါ ။

နောက်ပိုင်း `user` variable ကို သုံးတဲ့အခါ TypeScript Feedback တွေ သေချာရနိုင်သွားမှာပါ ။

<figure><img src="/files/1GQNLQSDN7qUqtO6erGt" alt=""><figcaption></figcaption></figure>

များသောအားဖြင့် ဘယ်အခြေအနေတွေမှာ Type Assertion နည်းကို အသုံးများလဲဆိုရင် ၊&#x20;

unknown type နဲ့ any type ရလာတဲ့ အခါမျိုးတွေမှာ&#x20;

ကိုယ်ကလည်း ဘယ် Type လဲဆိုတာ သေချာသိနေတဲ့ အခြေအနေမှာ သုံးပါတယ် ။
