Both interfaces and type aliases define object shapes, but they have key differences. Here's when to use each.
Basic Syntax#
Extension#
Declaration Merging#
Augmenting Libraries#
Primitive Types#
Union and Intersection#
Functions#
Classes#
Generics#
Recursive Types#
Performance#
Error Messages#
When to Use Each#
Best Practices#
Use Interface When:
✓ Defining object shapes
✓ Creating public APIs
✓ Need declaration merging
✓ Implementing in classes
Use Type When:
✓ Creating union types
✓ Working with primitives
✓ Using mapped/conditional types
✓ Creating type utilities
General Guidelines:
✓ Be consistent in your codebase
✓ Interface for objects, type for everything else
✓ Document complex types
✓ Prefer interface for public APIs
Avoid:
✗ Mixing without reason
✗ Over-engineering types
✗ Ignoring readability
✗ Complex nested intersections
Conclusion#
Both interfaces and types are powerful tools. Use interfaces for object shapes, class contracts, and public APIs. Use types for unions, primitives, tuples, and advanced type operations. For simple object types, either works - just be consistent. When in doubt, start with interface and switch to type if you need its unique features.