TypeScript interfaces and classes

TypeScript provides several features for building complex data models, including interfaces and classes. In this tutorial, we’ll explore the differences between these two constructs and how they can be used together to create powerful, reusable code.

Interfaces

An interface in TypeScript is a contract that describes the structure of an object. It defines the properties and methods that an object must have to satisfy the interface. Here’s an example:

interface Person {
  firstName: string;
  lastName: string;
  age: number;
  sayHello(): void;
}

In this example, we’ve defined an interface called Person that requires objects to have a firstName, lastName, age, and sayHello method. To implement this interface, we can create an object that meets these requirements:

const john: Person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  sayHello: () => {
    console.log("Hello, world!");
  },
};

Interfaces can be used to define the structure of objects passed between functions, making it easier to write robust and maintainable code.

Classes

A class in TypeScript is a blueprint for creating objects. It defines the properties and methods that objects of that class will have. Here’s an example:

class Rectangle {
  width: number;
  height: number;

  constructor(width: number, height: number) {
    this.width = width;
    this.height = height;
  }

  getArea(): number {
    return this.width * this.height;
  }
}

In this example, we’ve defined a class called Rectangle that has a width and height property, as well as a getArea method. To create an object of this class, we can use the new keyword:

const rect = new Rectangle(10, 20);
console.log(rect.getArea()); // Output: 200

Classes can be used to create reusable code and to model complex data structures.

Interfaces and Classes Together

Interfaces and classes can be used together to create more complex data models. For example, we can define an interface for a shape that requires a getArea method, and then implement that interface in various shape classes:

interface Shape {
  getArea(): number;
}

class Rectangle implements Shape {
  // implementation here
}

class Circle implements Shape {
  // implementation here
}

By using interfaces to define contracts and classes to provide implementations, we can create powerful, extensible code that’s easy to maintain.

Purpose of Interface and Classes

In TypeScript, interfaces and classes are both used to define the shape of an object, but they have different purposes and use cases.

Interfaces are used to describe the shape of an object and the contract that a class or object must implement. They are often used to define the types of function parameters or the shape of data that is returned by a function. Interfaces do not provide an implementation for the contract they describe.

Classes, on the other hand, are used to define the behavior and state of an object. They can contain properties, methods, and constructors, and provide an implementation for the contract that interfaces describe. Classes can be used to create new objects based on the blueprint that they define.

So when should you use an interface versus a class? Here are a few guidelines:

  • Use an interface when you want to define a contract that must be implemented by other classes or objects. For example, you might define an interface that describes the shape of a data object that is returned by a web API.

  • Use a class when you want to define a blueprint for creating new objects that share a common behavior and state. For example, you might define a class that represents a user in your application, with properties like name, email, and password.

  • Use both when you want to define a complex data model that has multiple levels of abstraction. For example, you might define an interface that describes the shape of a User object, and then implement that interface in a User class that provides an implementation for the contract.

In general, interfaces and classes are both powerful tools that can be used to write robust and maintainable code in TypeScript. By understanding the differences between them and their appropriate use cases, you can make better design decisions in your own projects.

Conclusion

TypeScript’s interfaces and classes provide powerful tools for building complex data models. By understanding the differences between these constructs and how they can be used together, you can write more robust and maintainable code in your own projects.