What is the difference between type and interface in TypeScript? How to use type and interface?

26 January 2023 364 Reading time: 51 second

In TypeScript, the main difference between type and interface is their intended use.

The type keyword allows an existing type to be renamed or merged into one. For example, in the following example, a "Person" type is created and contains a "name" and "age" properties:
 

type Person = { name: string, age: number };

The interface keyword defines an interface for classes or objects. This interface specifies which properties and methods should be contained within this class or object. For example, in the following example, a "Person" interface is created and includes a "name" and "age" properties:
 


interface Person {
    name: string;
    age: number;
}

The main difference between these two keywords is that type provides for renaming or combining a type, whereas interface provides for defining an interface. Typescript's type alias and interfaces are different. Interfaces specify what properties and methods a class or object must contain, while type alias allows a type to be renamed or combined.

Similar articles