Careeroza
Interface vs Type Alias
Comparison
| Feature | interface | type |
|---|---|---|
| Object shapes | yes | yes |
| Unions, tuples, primitives | no | yes: type Id = string | number |
| Extending | extends | intersection & |
| Declaration merging | yes (same name merges) | no (duplicate name is an error) |
| Mapped / conditional types | no | yes |
| Error messages | shows the interface name | sometimes shows the expanded type |
The Same Shape Both Ways
Example
interface CartItemI {
productId: number;
qty: number;
}
type CartItemT = {
productId: number;
qty: number;
};
const a: CartItemI = { productId: 1, qty: 2 };
const b: CartItemT = a; // same shape, fully compatible
console.log(b);
type PaymentMode = "UPI" | "CARD" | "COD"; // only a type alias can do this
const mode: PaymentMode = "UPI";
console.log(mode);Output
{ productId: 1, qty: 2 }
UPITip: A simple team rule: use interface for object shapes (especially public APIs and classes) and type for unions, tuples, function types and computed types. Most important is to be consistent.