DEV Community

David J Song
David J Song

Posted on

The Battle Between Interface and Type in TypeScript

Where the Fight Begins

TypeScript's interface and type aliases overlap for defining object shapes. Their key distinction is that interfaces support declaration merging while type aliases offer richer expressiveness for unions, intersections, mapped, and conditional types.

In modern practice, many teams default to type for its flexibility, using interface only when they need merging or classic OOP-style contracts. Performance between the two is now virtually identical, so clarity and project conventions determine the choice.

Core Differences

  • Declaration Merging: Only interfaces can be reopened and extended across multiple declarations, while types cannot.
  • Expressiveness: Types can define primitives, unions, intersections, mapped types, and more, while interfaces are limited to object/function signatures.
  • Compiler Impact: There is no meaningful compile-time speed difference in current TypeScript versions.

Industry Practices

  • Default to types: Many codebases adopt type as the standard for general type definitions and advanced type manipulations.
  • Reserve interfaces: Use when you specifically need extends, implements, or declaration merging for library augmentation or OOP contracts.

Recommendations

  1. Pick one by convention: Enforce your team's preferences via lint rules to ensure consistency.
  2. Use type for flexibility: Leverage it for complex scenarios where unions, conditional types, or utility types are needed.
  3. Use interface for merging: Opt for it when you need to augment or merge declarations, or when modeling class-like contracts.

Now You Know Which to Use

There is no absolute better choice. Choosing between interface and type should be guided by your project's style and needs. Choose type for its versatility, or interface when you require its unique merging capabilities or OOP semantics.

Top comments (0)