What is the difference between type and interface
· Category: TypeScript
Short answer
interface declares object shapes and supports declaration merging. type creates aliases for any type, including unions and primitives, but cannot be reopened.
Key differences
- Extensibility: Interfaces can be merged by declaring the same name multiple times. Types cannot.
- Unions:
typecan represent union types (type Status = 'active' | 'inactive'), whileinterfacecannot. - Implementation: Classes can implement both
interfaceandtypealiases that describe object shapes. - Performance: In general, interfaces produce better error messages and are slightly more performant during type checking in large projects.
When to use each
- Use
interfacefor public API shapes and when you expect declaration merging. - Use
typefor unions, tuples, mapped types, and complex type transformations. - Many teams default to
interfacefor objects and reservetypefor utility types.