Course contentsShow
← TypeScript
Lesson 373 of 1,7968. Interfaces vs Type AliasesFree lesson

Unions and Intersections: Type Aliases Shine

Type aliases can directly represent unions and intersections, while interfaces require composition or extension.

Unions and Intersections: Type Aliases Shine

You'll learn why type aliases are the natural choice when working with unions and intersections.

The Core Idea

While both interfaces and type aliases can represent complex types, type aliases have a direct, elegant syntax for unions (|) and intersections (&). Interfaces can achieve similar results through composition and extension, but they require more ceremony and don't support unions at all.

Type Aliases: Built for Unions and Intersections

With type aliases, you can directly express "either/or" (union) and "combined" (intersection) relationships:

// Union: A value can be string OR number
type ID = string | number;

// Intersection: Must satisfy BOTH types
type Timestamped = { createdAt: Date };
type Author = { author: string };
type TimestampedAuthor = Timestamped & Author;

const post: TimestampedAuthor = {
  createdAt: new Date(),
  author: "Alice"
};

Interfaces: Indirection Required

Interfaces cannot represent unions directly. For intersections, you'd use extension:

interface Timestamped { createdAt: Date }
interface Author { author: string }

// Extension achieves intersection-like behavior
interface TimestampedAuthor extends Timestamped, Author {}

// But there's NO interface syntax for unions
// You'd need to use a type alias anyway:
type ID = string | number;

When This Matters

If your domain involves tagged unions, discriminated types, or combining multiple shapes, type aliases provide cleaner, more direct syntax. Interfaces excel at declaration merging and class contracts, but unions and intersections are where type aliases truly shine.

Key Takeaway: Type aliases natively support union (|) and intersection (&) syntax, making them the idiomatic choice for these type compositions—interfaces cannot express unions and require extension for intersection-like behavior.