Difference between type and interface in TypeScript

· Category: TypeScript

Short answer

interface can be reopened to add properties via declaration merging, while type can represent unions, intersections, and primitives. Both can describe object shapes, but interfaces are preferred for public APIs. For related type concepts, see python type hints. For object-oriented patterns, see python classes objects.

Steps

  1. Define an interface: interface User { name: string; }
  2. Merge declarations: redeclare interface User to add email: string
  3. Define a type alias: type ID = string | number
  4. Use intersections: type Employee = Person & { salary: number }
  5. Choose interface for object shapes you may need to extend

Tips

  • Use type for unions, tuples, and mapped types
  • Use interface when you expect third-party extensions
  • For testing your types, see python testing with pytest