DEV Community

Rajae Robinson
Rajae Robinson

Posted on • Originally published at bluesockets.com

3

When to Use Interface vs. Class in TypeScript

Hey fellow TS Devs!

I wrote an insightful article on when to use interfaces and classes in TypeScript. It's a crucial decision we often face, and this article breaks it down nicely.

Understanding Interfaces:
Interfaces are like contracts for objects, defining their structure and behavior. They ensure consistency in your codebase. Here's an example:

interface Person {
  firstName: string;
  lastName: string;
  age: number;
}
Enter fullscreen mode Exit fullscreen mode

Use Cases for Interfaces:

  1. Defining Object Shapes: When you need specific properties and their types, interfaces got your back.

  2. Implementing Contracts: Great for enforcing rules and common functionality across classes.

  3. Extending Interfaces: You can build specialized interfaces by extending existing ones.

Exploring Classes:
Classes are fundamental to object-oriented programming. They encapsulate data and behavior in one unit.

Use Cases for Classes:
1.Creating Instances: If you need multiple instances of something (like users in a system), classes are your go-to.

class User {
  username: string;
  password: string;

  constructor(username: string, password: string) {
    this.username = username;
    this.password = password;
  }

  login() {
    // Logic for user login
  }
}
Enter fullscreen mode Exit fullscreen mode

2.Inheritance and Polymorphism: Classes help build hierarchies with shared characteristics.

class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  makeSound() {
    // Default sound
  }
}

class Dog extends Animal {
  makeSound() {
    return 'Bark!';
  }
}

class Cat extends Animal {
  makeSound() {
    return 'Meow!';
  }
}
Enter fullscreen mode Exit fullscreen mode

3.Encapsulation: Secure and maintainable code by controlling access to properties and methods.

class BankAccount {
  private balance: number = 0;

  deposit(amount: number) {
    this.balance += amount;
  }

  withdraw(amount: number) {
    if (amount <= this.balance) {
      this.balance -= amount;
    } else {
      console.log('Insufficient funds');
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Choosing between an Interface and a Class:
It all depends on the problem at hand. Often, they complement each other. Interfaces define shapes, while classes provide instantiable blueprints with behavior.

If you want to dive deeper, check out the full article. It's a great read for writing more maintainable, scalable, and readable code in TypeScript.

Happy coding! 🚀

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up