Sobes.tech
Middle+

What is the difference between 'type' and 'interface' in TypeScript?

sobes.tech AI

Answer from AI

In TypeScript, type and interface have similar purposes — describing types, but there are differences:

  • interface is intended for describing the structure of objects and can be extended (extends) or implemented (implements) by classes.
  • type is more versatile, allowing the creation of aliases for primitives, unions, intersections, and other types.

Example:

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

type ID = string | number;

// interfaces can be extended
interface IEmployee extends IUser {
  salary: number;
}

// types can be combined
type Employee = IUser & { salary: number };

Also, interfaces support declaration merging, whereas types do not.

Overall, interfaces are often recommended for describing objects, while types are suitable for complex types.

What is the difference between 'type' and 'interface'… - sobes.tech