You are currently viewing Interface vs Type
In TypeScript, there are two ways to define object shapes: interfaces and types. While they may seem similar, they have some key differences and specific use cases. This article will help you understand when to use interfaces and types in TypeScript.

Interface vs Type

Interface vs Type

In TypeScript, there are two ways to define object shapes: interfaces and types. While they may seem similar, they have some key differences and specific use cases. This article will help you understand when to use interfaces and types in TypeScript.

Interfaces

Interfaces are a powerful feature of TypeScript that allow you to define contracts for object shapes. They can describe the properties and methods that an object should have.

interface Person {
  name: string;
  age: number;
  greet(): void;
}

const john: Person = {
  name: 'John',
  age: 30,
  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

john.greet();

Interfaces can also be extended and implemented:

interface Animal {
  name: string;
  eat(food: string): void;
}

interface Dog extends Animal {
  bark(): void;
}

class Labrador implements Dog {
  name: string;

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

  eat(food: string) {
    console.log(`Labrador ${this.name} is eating ${food}`);
  }

  bark() {
    console.log('Woof!');
  }
}

const myLabrador = new Labrador('Max');
myLabrador.eat('bones');
myLabrador.bark();

Interfaces are often used for defining contracts that multiple objects can conform to, and they’re commonly used in object-oriented programming.

Types

Types, on the other hand, are a more general way to define object shapes in TypeScript. They can describe any valid type, including primitives, arrays, unions, and more.

type Point = {
  x: number;
  y: number;
};

const point: Point = {
  x: 10,
  y: 20
};

Types can also be composed and used in combination with other types:

type User = {
  name: string;
  age: number;
};

type Address = {
  street: string;
  city: string;
};

type Person = User & Address;

const person: Person = {
  name: 'John',
  age: 30,
  street: '123 Main St',
  city: 'New York'
};

Types are often used for creating complex types that combine several smaller types together.

When to Use Interfaces

Use interfaces when:

  • You need to define a contract that multiple objects should adhere to.
  • You want to extend or implement an existing interface.
  • You’re working in an object-oriented programming paradigm.

When to Use Types

Use types when:

  • You need to describe a shape that is not an object, such as a union or literal type.
  • You’re creating complex types by combining smaller types together.
  • You don’t need the additional features provided by interfaces.

Conclusion

In TypeScript, both interfaces and types have their place. Interfaces are more suitable when defining contracts, while types offer more flexibility. Understanding the differences and knowing when to use each will make your code more expressive and maintainable.