FrontendPrep
Menu
Topics
Questions
Guides
Challenges
Soon
Back to TypeScript Questions
typescriptEasy

TypeScript: Interface vs. Type Alias

Master TypeScript types. Learn the differences between Interface and Type declarations, when to use which, declaration merging, mapped types, and TS compiler performance.

TypeScript: Interface vs. Type Alias

One of the most common TypeScript interview questions for web developers is:

What is the difference between an interface and a type alias in TypeScript? When should you choose one over the other?

To answer this, you need to understand compile-time differences, declaration merging, and capabilities unique to type aliases.


1. Core Differences

In modern versions of TypeScript, interface and type have become very similar. However, key differences remain:

A. Declaration Merging

  • Interface: Interfaces support declaration merging. If you declare two interfaces with the same name in the same scope, TypeScript automatically merges their property definitions.
    interface Window {
      title: string;
    }
    interface Window {
      isMinimised: boolean;
    }
    // Resulting interface has both title and isMinimised
  • Type Alias: Type aliases cannot be declared multiple times with the same name. Attempting to do so yields a duplicate identifier compile error.

B. Mapped and Utility Types

  • Type Alias: Can represent unions, intersections, primitives, tuples, and mapped types:
    type Point = { x: number; y: number };
    type ID = string | number; // Union types
    type Keys = "name" | "age";
    type Flags = { [K in Keys]: boolean }; // Mapped type
  • Interface: Cannot represent union or primitive mappings directly. They must always represent object shapes.

2. Syntax Comparison

Extending/Inheritance

  • Interface extends using the extends keyword:
    interface Animal { name: string }
    interface Dog extends Animal { bark(): void }
  • Type Alias achieves inheritance via intersection (&):
    type Animal = { name: string };
    type Dog = Animal & { bark(): void };

3. Which One Should You Use?

TypeScript official documentation recommends:

  • Use interface by default for defining the public shape of objects, library APIs, and client-server payloads, as they are slightly faster for the TS compiler to typecheck and offer clear inheritance warnings.
  • Use type when you need union structures, tuples, intersection combinations, mapped configurations, or utility helper wrappers.

Key Takeaways

  • Declaration Merging: Makes interfaces ideal for extendable library definitions.
  • Type Aliases: Required for union definitions (A | B), intersections, and advanced types.
  • Style Consistency: Choose one style consistently across your team's project guidelines.

Share this Resource

Help other developers level up by sharing this study guide.

More Technical Questions

Expand your mastery. Deep dive into other frontend interview challenges in this category.