DEV Community

Cover image for Types vs Interfaces: The Ultimate Showdown 🥊
Ravin Rau
Ravin Rau

Posted on

Types vs Interfaces: The Ultimate Showdown 🥊

I am trying a new a way of writing with this article. Do comment if you feel it is interesting and it adds values while you are reading it.

Summary

type vs interface

Choose type when:

  • You need primitive type aliases
  • You're working with unions or intersections
  • You need tuple types
  • You're doing complex type manipulations
  • You want to prevent declaration merging

Choose interface when:

  • You're writing object-oriented code
  • You need declaration merging
  • You're creating public APIs
  • You're working with classes and implementations
  • You want slightly better compiler performance

Understanding TypeScript: The Foundation

Before we dive into the most epic battle in the TypeScript universe, let's understand what makes TypeScript so special. Imagine JavaScript, but with superpowers – that's TypeScript in a nutshell. Created by Microsoft, TypeScript is a strongly typed programming language that builds on JavaScript, adding optional static typing and class-based object-oriented programming.

Think of TypeScript as your friendly neighborhood code guardian. While JavaScript lets you do whatever you want (sometimes leading to unexpected bugs), TypeScript keeps you in check by ensuring your code follows specific type rules. It's like having a smart assistant that catches potential errors before they happen in production.

For example, in regular JavaScript, this would work (but might cause problems later):

// JavaScript
function addNumbers(a, b) {
    return a + b;
}
addNumbers("5", 10); // Returns "510" - Probably not what we wanted!
Enter fullscreen mode Exit fullscreen mode

But TypeScript helps prevent such issues:

// TypeScript
function addNumbers(a: number, b: number): number {
    return a + b;
}
addNumbers("5", 10); // Error: Argument of type 'string' is not assignable to parameter of type 'number'
Enter fullscreen mode Exit fullscreen mode

The Great Debate: Types vs Interfaces

Ever found yourself staring at your code editor, cursor blinking, wondering whether to use type or interface? You're not alone. This represents one of the most epic battles in the TypeScript universe, and today we're going to explore and understand about this debate.


Round 1: The Basics - Getting to Know Our Character

Let's start with something simple. Both types and interfaces can define basic object shapes:

// Interface doing its thing
interface SuperHero {
  name: string;
  power: string;
  age: number;
}

// Type showing off its moves
type SuperHero = {
  name: string;
  power: string;
  age: number;
}

// Both work perfectly fine! No winner yet! 
const spiderman: SuperHero = {
  name: "Peter Parker",
  power: "Web-slinging",
  age: 23
}
Enter fullscreen mode Exit fullscreen mode

Round 2: Primitive Powers - The First Knockout! 🥊

In programming, primitive types are the most basic data types that serve as the building blocks for more complex data structures. Think of them like the atoms of programming – they're the simplest elements that can't be broken down further.

In TypeScript, the main primitive types are:

// Number - represents both integers and floating-point numbers
type Score = number;       // Could be 100, -5, or 3.14
let gameScore: Score = 100;

// String - represents text data
type Name = string;        // Any text value
let playerName: Name = "Alex";

// Boolean - represents true/false values
type IsGameOver = boolean; // Can only be true or false
let gameEnded: IsGameOver = false;

// Undefined - represents an uninitialized value
type NotYetSet = undefined;

// Null - represents an intentionally absent value
type NoData = null;

// Symbol - represents a unique identifier
type UniqueKey = symbol;

// BigInt - represents very large integers
type HugeNumber = bigint;
Enter fullscreen mode Exit fullscreen mode

Now, here's why type has an advantage over interface when working with primitives:

// ✅ This works great with type
type PlayerName = string;
let name: PlayerName = "John"; // Perfect!

// ❌ This doesn't work with interface
interface PlayerName extends string {}
// TypeScript Error: An interface cannot extend a primitive type like 'string'
Enter fullscreen mode Exit fullscreen mode

The reason for this difference lies in how TypeScript was designed:

  1. type was created to be a flexible way to name any kind of type, including primitives
  2. interface was designed specifically for describing object shapes and contracts

Here's a practical example showing why primitive types are useful:

// Creating meaningful aliases for primitive types
type UserId = string;
type Age = number;
type IsActive = boolean;

// This makes our code more readable and maintainable
function createUser(id: UserId, age: Age, active: IsActive) {
    // The types tell us exactly what we expect
    // Even though they're just primitives, the names add meaning
    return { id, age, active };
}

// TypeScript ensures we pass the right types
createUser("user123", 25, true);     // ✅ Works
createUser(123, "25", "true");       // ❌ Error - wrong types!
Enter fullscreen mode Exit fullscreen mode

The real power comes when we combine primitive types to create more complex types:

// Union types with primitives
type Status = "pending" | "success" | "error";  // Only these three strings are allowed
type AgeGroup = number | "unknown";             // Can be a number or the string "unknown"

// Using our union types
function processStatus(status: Status) {
    switch (status) {
        case "pending": return "";
        case "success": return "";
        case "error": return "";
        // TypeScript knows these are all possible cases!
    }
}

// TypeScript will catch invalid values
processStatus("pending");   // ✅ Works
processStatus("invalid");   // ❌ Error - not a valid status!
Enter fullscreen mode Exit fullscreen mode

The beauty of using type with primitives is that it adds semantic meaning to our code while maintaining type safety. Instead of seeing just string or number, we see UserId or Age, which makes our code more self-documenting and easier to understand.

Remember: While both type and interface are powerful features in TypeScript, when it comes to working with primitive types, type is your best friend. It provides the flexibility and expressiveness needed to work with these basic building blocks of programming.

Our winner for this round: type! 🏆


Round 3: Declaration Merging - Interface's Secret Weapon! 🥷

Declaration merging is the ability to combine multiple declarations with the same name into a single definition. Think of it like building a LEGO structure. With interfaces, you can keep adding new pieces (declarations) to your existing structure, and TypeScript will automatically combine them all together.

This is incredibly useful in real-world scenarios. Below is a practical example of why declaration merging is valuable:

// In your application's core types file
interface User {
  id: string;
  name: string;
}

// In a feature-specific file
interface User {
  preferences: {
    theme: 'light' | 'dark';
    notifications: boolean;
  }
}

// In another feature file
interface User {
  cart: {
    items: string[];
    total: number;
  }
}

// TypeScript merges all these into one complete User interface!
const user: User = {
  id: "123",
  name: "John",
  preferences: { theme: 'dark', notifications: true },
  cart: { items: ['item1'], total: 29.99 }
}
Enter fullscreen mode Exit fullscreen mode

Now, why can't types do this? The reason lies in how TypeScript treats type aliases. When you create a type, you're creating an alias - essentially giving a name to a specific type definition. It's like creating a label for a specific recipe - you can't have two different recipes with the same label, it would be ambiguous!

// This is like saying "Recipe A is for cake"
type Recipe = {
  ingredients: string[];
}

// This causes an error because it's like trying to say
// "Recipe A is for cookies" - which one is it?
type Recipe = {
  cookingTime: number;
}
Enter fullscreen mode Exit fullscreen mode

Think of it this way:

  • Interfaces are like building plans that can be extended and modified
  • Types are like final blueprints that can't be changed once defined

Here's another real-world example where declaration merging shines - extending third-party libraries:

// Original library definition
interface Window {
  title: string;
}

// Your application's additions
interface Window {
  analytics: {
    trackEvent(event: string): void;
  }
}

// Now you can use your analytics anywhere:
window.analytics.trackEvent('page_view');
Enter fullscreen mode Exit fullscreen mode

This is particularly powerful because:

  1. You can extend existing types without modifying the original source
  2. Different parts of your application can contribute to the same interface
  3. It works well with module augmentation patterns

One thing to note though - while declaration merging is powerful, it should be used thoughtfully. Too much merging can make your code harder to follow, as the complete interface definition is spread across multiple locations.

Winner of this round: interface! 🏆


Round 4: Complex Type Gymnastics 🤸‍♂️

First, let's understand mapped types using a simple analogy. Imagine you have a blueprint for a house, and you want to create a new blueprint where every room is marked as "locked." That's essentially what a mapped type does - it takes an existing type and transforms all its properties in a consistent way.

Here's the ReadOnly mapped type broken down:

type ReadOnly<T> = {
  readonly [P in keyof T]: T[P];
}

// Let's see it in action with a real example:
interface User {
  name: string;
  age: number;
}

// Using our ReadOnly mapped type
type ReadOnlyUser = ReadOnly<User>;
// This creates:
// {
//   readonly name: string;
//   readonly age: number;
// }

const user: ReadOnlyUser = {
  name: "John",
  age: 30
};
// Now we can't modify these properties
user.name = "Jane"; // Error! Cannot assign to 'name' because it is a read-only property
Enter fullscreen mode Exit fullscreen mode

Next, let's look at conditional types. Think of these like "if statements" but for types. They let us make decisions about types at compile time:

type IsString<T> = T extends string ? true : false;

// Let's see how it works:
type Result1 = IsString<"hello">; // type Result1 = true
type Result2 = IsString<42>;      // type Result2 = false

// A more practical example:
type ArrayOrString<T> = T extends any[] 
  ? "This is an array"
  : "This is not an array";

type Test1 = ArrayOrString<string[]>;  // "This is an array"
type Test2 = ArrayOrString<string>;    // "This is not an array"
Enter fullscreen mode Exit fullscreen mode

The Pick utility type is particularly powerful. It lets us select specific properties from an existing type:

type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
}

// Here's a practical example:
interface Article {
  title: string;
  content: string;
  author: string;
  publishDate: Date;
  views: number;
}

// Maybe we only need title and author for a preview
type ArticlePreview = Pick<Article, 'title' | 'author'>;
// Results in:
// {
//   title: string;
//   author: string;
// }
Enter fullscreen mode Exit fullscreen mode

Finally, tuple types provide a way to define arrays with a fixed number of elements where each element can have its own type:

type Coordinate = [number, number];
// This ensures exactly two numbers:
const valid: Coordinate = [10, 20];      // ✅ OK
const invalid: Coordinate = [10, 20, 30]; // ❌ Error: Too many elements
const wrong: Coordinate = [10, "20"];     // ❌ Error: Second element must be number
Enter fullscreen mode Exit fullscreen mode

Why can't interfaces do these tricks? The answer lies in how interfaces were designed. Interfaces were created primarily to define contracts for object shapes - they're great at describing what properties and methods an object should have. However, they weren't designed to:

  1. Transform other types (like mapped types do)
  2. Make decisions based on type information (like conditional types)
  3. Create complex type relationships (like utility types)
  4. Define strict tuple structures with specific element types

Here's what happens if we try to replicate these patterns with interfaces:

// ❌ This isn't possible with interfaces
interface ReadOnlyVersion<T> {
    readonly [P in keyof T]: T[P];  // Error: Interfaces can't use mapped types
}

// ❌ Can't do conditional logic
interface StringCheck<T> extends (T extends string ? true : false) {} // Error

// ❌ Can't create exact tuple types
interface Coordinate extends Array<number> {
    length: 2;  // We can specify length but can't enforce it as strictly as tuple types
}
Enter fullscreen mode Exit fullscreen mode

This is why type is the go-to choice when you need to work with more complex type transformations and relationships. It provides a more expressive and flexible system for advanced type operations, while interfaces excel at their primary purpose of defining object shapes and contracts.

Our winner for this round: type! 🏆

Round 5: Object-Oriented Programming - Interface's Home Turf 🏠

In OOP, we often want to define contracts that classes must follow – think of these like blueprints for buildings. Interfaces were specifically designed for this purpose, making them more natural and intuitive for OOP patterns. Below are an real-world example:

// First, let's see how interfaces work in OOP
interface Vehicle {
  start(): void;
  stop(): void;
  fuelLevel: number;
}

interface ElectricVehicle extends Vehicle {
  batteryLevel: number;
  charge(): void;
}

// Notice how clean and natural this implementation looks
class Tesla implements ElectricVehicle {
  fuelLevel = 0;  // Always 0 for electric cars
  batteryLevel = 100;

  start() {
    console.log("Silent start... 🔌");
  }

  stop() {
    console.log("Silent stop... ⚡");
  }

  charge() {
    console.log("Charging at supercharger...");
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, if we try to achieve the same thing with types, it becomes more cumbersome:

// With types, we have to use intersection types
type Vehicle = {
  start(): void;
  stop(): void;
  fuelLevel: number;
}

// We need to use & for inheritance instead of 'extends'
type ElectricVehicle = Vehicle & {
  batteryLevel: number;
  charge(): void;
}

// The implementation looks the same, but we lose some key benefits
class Tesla implements ElectricVehicle {
  // Same implementation as before
}
Enter fullscreen mode Exit fullscreen mode

Here's why interfaces are superior for OOP:

  1. Natural Extension Syntax:
// Interfaces use the familiar 'extends' keyword
interface Car extends Vehicle {
  numberOfDoors: number;
}

// Types must use intersection operators
type Car = Vehicle & {
  numberOfDoors: number;
}
Enter fullscreen mode Exit fullscreen mode
  1. Declaration Merging in Class Context:
// You can add methods to interfaces across files
interface Vehicle {
  start(): void;
}
// In another file
interface Vehicle {
  park(): void;
}

// A class implementing Vehicle must implement both methods
class Car implements Vehicle {
  start() { /* ... */ }
  park() { /* ... */ }
}
Enter fullscreen mode Exit fullscreen mode
  1. Better Error Messages:
// With interfaces, TypeScript provides clearer error messages
class BrokenCar implements Vehicle {
  // TypeScript will tell you exactly which methods are missing
  // from the Vehicle interface
}
Enter fullscreen mode Exit fullscreen mode

Think of it like building with LEGO blocks:

  • Interfaces are like official LEGO pieces that are designed to snap together perfectly
  • Types are like generic building blocks that can be made to work together but require more effort

Let's see a more complex example that showcases why interfaces are preferred in OOP:

interface Database {
  connect(): Promise<void>;
  query(sql: string): Promise<any[]>;
  close(): Promise<void>;
}

interface CachingDatabase extends Database {
  getCached(key: string): Promise<any>;
  setCached(key: string, value: any): Promise<void>;
}

// Clean implementation with clear contract
class PostgresWithCache implements CachingDatabase {
  private cacheStore = new Map();

  async connect() {
    console.log("Connecting to Postgres...");
  }

  async query(sql: string) {
    console.log("Executing query:", sql);
    return [];
  }

  async close() {
    console.log("Closing connection...");
  }

  async getCached(key: string) {
    return this.cacheStore.get(key);
  }

  async setCached(key: string, value: any) {
    this.cacheStore.set(key, value);
  }
}
Enter fullscreen mode Exit fullscreen mode

The interface-based approach gives us clear contracts, better tooling support, and more maintainable code in an OOP context. While types can technically achieve similar results, they weren't designed with OOP in mind, making them less elegant for these use cases.

Our winner for this round: interface! 🏆

Performance Showdown! ⚡

Here's a fun fact: in terms of runtime performance, both interface and type have nothing. That's right - they both disappear completely during compilation. It's like they were never there!

However, during development:

  • Interfaces are slightly faster to check (they've got some clever caching tricks)
  • Types are more flexible but might take a tiny bit longer to process (especially with complex unions)

So, Who Wins? 🏆

Plot twist: They're both winners!


The Pro Tips 🎯

  1. Start with interface for objects unless you need specific type features
  2. Use type for function types (they're more readable)
  3. Be consistent within your project
  4. Let your team's preferences guide you
  5. Remember: both are awesome in their own ways!

Final Thoughts 🎭

Think of type and interface as different tools in your TypeScript toolbox. Sometimes you need a hammer (interface), sometimes you need a Swiss Army knife (type). The key is knowing when to use each one!

Remember: The best code is the one that your team can understand and maintain easily. So pick the approach that makes the most sense for your specific situation, and don't be afraid to mix and match when it makes sense!

Comment below on which one do you prefer, types or interfaces and which one do you use more often ?

Top comments (1)

Collapse
 
getvast profile image
James Harrison

Great article! Thanks for sharing. Will probably need to revisit this next time I'm debating which to use!