Courses/TypeScript/Interfaces and Type Aliases

    Lesson 3 • Beginner

    Interfaces and Type Aliases 📐

    Define the shape of objects, create reusable custom types, and master union/intersection types — the foundation of scalable TypeScript code.

    What You'll Learn in This Lesson

    • • Interfaces with optional, readonly, and extended properties
    • • Type aliases for unions, intersections, and primitives
    • • When to use interface vs type
    • • Index signatures and function type interfaces
    • • Discriminated unions for type-safe state management

    1️⃣ Interfaces

    Interfaces define the structure (shape) of objects. They specify which properties must exist, their types, and whether they're optional or readonly. Interfaces can extend other interfaces for composition.

    Try It: Interfaces

    Optional, readonly, extended, and composed interfaces

    Try it Yourself »
    JavaScript
    // Interfaces — Define Object Shapes
    console.log("=== Basic Interface ===");
    console.log();
    console.log("interface User {");
    console.log("  id: number;");
    console.log("  name: string;");
    console.log("  email: string;");
    console.log("  isActive: boolean;");
    console.log("}");
    console.log();
    
    let user = { id: 1, name: "Alice", email: "alice@example.com", isActive: true };
    console.log("const user: User = " + JSON.stringify(user, null, 2));
    console.log();
    
    // Optional properties
    console.log("=== Opti
    ...

    2️⃣ Type Aliases

    Type aliases create named types for any shape — including unions (A | B), intersections (A & B), primitives, tuples, and function types. They're more flexible than interfaces.

    Try It: Type Aliases

    Unions, intersections, literal types, and interface vs type comparison

    Try it Yourself »
    JavaScript
    // Type Aliases — Flexible Custom Types
    console.log("=== Type Aliases ===");
    console.log();
    console.log("type Point = { x: number; y: number; };");
    console.log("type ID = string | number;");
    console.log("type Callback = (data: string) => void;");
    console.log();
    
    // Union types
    console.log("=== Union Types (|) ===");
    console.log("type Status = 'active' | 'inactive' | 'pending';");
    console.log();
    let statuses = ["active", "inactive", "pending"];
    for (let s of statuses) {
      console.log("  status = 
    ...

    3️⃣ Advanced Patterns

    Discriminated unions (tagged unions) are one of TypeScript's most powerful patterns. By adding a kind property, TypeScript can narrow types in switch statements — enabling safe, exhaustive handling of all cases.

    Try It: Advanced Patterns

    Index signatures, function interfaces, and discriminated unions

    Try it Yourself »
    JavaScript
    // Advanced Interface Patterns
    console.log("=== Index Signatures ===");
    console.log("interface StringMap {");
    console.log("  [key: string]: string;  // any string key → string value");
    console.log("}");
    let env = { NODE_ENV: "production", API_KEY: "abc123", PORT: "3000" };
    console.log("const env: StringMap = " + JSON.stringify(env));
    console.log();
    
    console.log("=== Function Interfaces ===");
    console.log("interface Formatter {");
    console.log("  (input: string): string;");
    console.log("}");
    conso
    ...

    ⚠️ Common Mistakes

    ⚠️
    Excessive optional properties — If most properties are optional, you may need separate interfaces for creation vs. display.
    ⚠️
    Not using discriminated unions — Modeling state as { loading: boolean; error: string | null; data: T | null } allows impossible states. Use discriminated unions instead.
    💡
    Pro Tip: Use interface for public APIs (they can be augmented). Use type for internal utility types.

    📋 Quick Reference

    PatternSyntax
    Interfaceinterface User { name: string }
    Optionalbio?: string
    Readonlyreadonly id: number
    Uniontype ID = string | number
    Intersectiontype A = B & C
    Literaltype Dir = 'up' | 'down'

    🎉 Lesson Complete!

    You can define any object shape! Next, learn to type functions and write flexible generic code.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service