Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingintermediate

Union Types & Aliases

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Union Types: Variable တစ်ခုက type တစ်ခုထက်ပို၍ ဖြစ်နိုင်သောအခါ pipe (|) character ဖြင့် သတ်မှတ်နိုင်သည်။

Type Aliases: type keyword ဖြင့် custom type name အသစ်တစ်ခု ဖန်တီးနိုင်သည်။

typescript
// Type Alias for a complex type
type StringOrNumber = string | number;

function printId(id: StringOrNumber) {
  console.log("Your ID is: " + id);
}

printId(101);
printId("abc-101");
You should see
Your ID is: 101 Your ID is: abc-101
Union Types & Aliases | Thuta Learning