> 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="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FlpQDvhkfRCVuFumFMzmn%2FTypescript%20Casting%20Syntax.png?alt=media&amp;token=adc0bf4f-39cc-479a-8bb9-68bece2f4e26" alt=""><figcaption></figcaption></figure>

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

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

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

<figure><img src="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FpM5LS48zXYHXUk7dX6jZ%2FTypescript%20Casting%20Example%20Code%201.png?alt=media&amp;token=f4ba9dd0-329a-41bc-8a1f-133012a1a075" 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="https://4062235122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7tekcoDYDaKhlNreT5uK%2Fuploads%2FrXxAhv5d9U18vvS1vRzm%2FTypescript%20Casting%20Example%20Code%202.png?alt=media&amp;token=abad2b85-c9b1-461c-bf3b-08745a9d9d15" alt=""><figcaption></figcaption></figure>

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

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

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