Aliasing Primitive and Literal Types
What you'll learn: How to use type aliases to give meaningful names to primitives, literal values, and unions of literals.
Why Name Simple Types?
You've learned that type aliases use the type keyword to create reusable type names. But type aliases aren't just for complex objects—they can name primitives and literal types too.
Think of this like creating a variable that stores a label rather than data. Instead of writing string everywhere you need a user ID, you can create type UserID = string. Now UserID communicates intent, not just structure.
Aliasing Primitives
You can give semantic meaning to basic types:
type Count = number;
type Message = string;
type IsActive = boolean;
let score: Count = 42;
let greeting: Message = "Hello";
let enabled: IsActive = true;
This doesn't add runtime safety (a Count is still just a number), but it makes your code self-documenting.
Aliasing Literal Types
Type aliases really shine with literal types—types that represent exact values:
type Status = 'active' | 'inactive';
type Direction = 'north' | 'south' | 'east' | 'west';
type Zero = 0;
let userStatus: Status = 'active'; // ✓ Valid
// userStatus = 'pending'; // ✗ Error: not in union
Here, Status isn't just "any string"—it's exactly 'active' or 'inactive'. This prevents typos and invalid states.
When to Use This Pattern
Alias primitives when the meaning matters more than the type. Use literal unions when you have a fixed set of valid values, like configuration modes, API response states, or enumeration-like constants.
Key Takeaway: Type aliases let you name primitives like type ID = string for clarity, and literal unions like type Status = 'active' | 'inactive' to restrict values to exact options, making your code more expressive and safer.