Thuta Learning
AdvancedProgrammingintermediate

Union Types & Aliases

Relax. We'll talk through this in plain words — no textbook voice.

Union Types: When a variable could be more than one type, you mark that with the pipe (|) character.

Type Aliases: type keyword lets you create a brand-new 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