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
- Define an interface:
interface User { name: string; } - Merge declarations: redeclare
interface Userto addemail: string - Define a type alias:
type ID = string | number - Use intersections:
type Employee = Person & { salary: number } - Choose interface for object shapes you may need to extend
Tips
- Use
typefor unions, tuples, and mapped types - Use
interfacewhen you expect third-party extensions - For testing your types, see python testing with pytest