Interface And Type Aliases

Interface

An interface is a way to define a contract for the shape of an object. An interface specifies the names, types, and number of properties and methods that an object should have, and any object that implements the interface must have those properties and methods with the specified types.
For example, if you have the following interface:
interface Person {
  name: string;
  age: number;
  greet(): string;
}
This interface defines a contract for an object that represents a person. The object must have a name property that is a string, an age property that is a number, and a greet() method that returns a string. Any object that implements this interface must have these properties and methods with the specified types.
To implement an interface in TypeScript, you use the implements keyword in the class declaration. For example, you could create a Person class that implements the Person interface like this:
class Person implements Person {
  constructor(public name: string, public age: number) {}

  greet(): string {
    return `Hello, my name is ${this.name}`;
  }
}
In this code, the Person class implements the Person interface by having name and age properties with the same types as specified in the interface, and by having a greet() method with the same return type as specified in the interface.
Interfaces are a useful tool in TypeScript because they allow you to define a contract for the shape of an object. This can help to ensure that your code is correct and free of type errors, and it can also make your code more reusable and flexible. By using interfaces, you can define the structure of an object and create objects that conform to that structure.

Type Alias

A type alias is a way to give a new name to an existing type. This can be useful if you have a complex type that you want to use multiple times in your code, but you want to give the type a more descriptive or concise name.
To create a type alias in TypeScript, you use the type keyword followed by the new name for the type and the = operator, followed by the existing type. For example, if you have the following type:
type Person = {
  name: string;
  age: number;
}
This creates a new type alias called Person that is equivalent to the type {name: string; age: number;} . This means that you can use the Person type alias wherever you would use the original type.
Type aliases can be very simple, such as when you want to give a new name to a primitive type like string or number . For example, you could create a type alias called ID for the string type like this:
type ID = string;
In this case, the ID type alias is equivalent to the string type, so you can use it wherever you would use string.
Type aliases are also useful for typing callback functions. For example, if you have a function that accepts a callback as an argument, you can use a type alias to specify the type of the callback. For example:
type Callback = (data: string) => void;
function doSomething(cb: Callback) {
  // ...
}
In this code, the Callback type alias is used to define the type of the cb argument to the doSomething() function. The Callback type is a function that takes a string as an argument and returns nothing ( void ).
Overall, TypeScript’s type alias feature allows you to give a new name to an existing type, which can make your code more readable and concise. Type aliases are useful for creating simple aliases for primitive types and for defining the types of callback functions.

When to use which

Both interfaces and type aliases can be used to define the shape of an object. However, there are some differences between the two that can influence which one you should use in a given situation.
Interfaces are more powerful and flexible than type aliases, because they can extend other interfaces, and be implemented by classes. This makes interfaces well-suited for defining the structure of complex objects that may have multiple implementations.
On the other hand, type aliases are simpler and more concise than interfaces. They cannot be extended so easily, but this can make them easier to use and understand in some situations. Type aliases are especially useful for creating aliases for primitive types and for defining the types of callback functions.
In general, you should use an interface when you want to define a complex type with optional properties, multiple implementations, or inheritance. You should use a type alias when you want to create a simple alias for an existing type or when you want to define the type of a callback function.