> 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/other-topic/typescript-predicate-guard.md).

# TypeScript Predicate Guard

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

"is" Type Predicates ကို type narrowing လုပ်ဖို့ အသုံးပြုပါတယ်။

function တစ်ခုက parameter တစ်ခုကို လက်ခံပြီး Narrowing လုပ်ဖို့အတွက် ကိုယ်ပြောင်းလဲချင်တဲ့ Target Type တစ်ခုကို သတ်မှတ်ပေးလိုက်တာဖြစ်ပါတယ် ။

ရေးသားနည်း Syntax အရ `(param: type) => param is TargetType ;` ဖြစ်ပေ့မယ်&#x20;

အမှန်တကယ် Return type က Boolean ပဲ ပြန်ရပါမယ် ။

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

နမူနာ Code မှာ `checkIsAdmin` ဆိုတဲ့ function ရှိပါတယ် ။&#x20;

ရည်ရွယ်ချက်က checkIsAdmin ကို ခေါ်လိုက်တာနဲ့ လက်ခံလိုက်တဲ့ user parameter အခြေအနေက true ဖြစ်ခဲ့ရင် သူ့ရဲ့ `Type` ကို AdminUser type အဖြစ်ပြောင်းပေးလိုက်မှာပါ ။

`actionForAdmin` မှာ လက်ခံထားတဲ့ user parameter က အစမှာ AdminUser (သို့မဟုတ် | ) Normal User ပါ ။

if ( checkIsAdmin(user) ) block အတွင်းမှာ user ကို mouse ဖြင့် ထောက်ပြီး ကြည့်ကြည့်တဲ့အခါ AdminUser type အဖြစ် Assertion လုပ်သွားမှာဖြစ်ပါတယ် ။
