Classes vs. Interfaces and Types
What
TypeScript offers three primary ways to define the structure of an object: class, interface, and type.
How
- Classes: Provide both a type and a runtime implementation. They support logic, privacy, and constructors.
- Interfaces: Provide a purely static contract. They support extension and declaration merging.
- Types: The most flexible option, supporting unions, intersections, and primitives.
Why
Classes are best when you need to package logic alongside your data or use traditional OOP patterns. Interfaces and types are better for purely descriptive data (like API responses) because they have zero runtime overhead and are generally simpler to refactor.
Summary
Use types and interfaces for data-only structures, and reach for classes when you need behavior, privacy, or inheritance.