Code Quality
TypeScript `interface` vs `type`: Key Differences
TypeScript has two ways to define object shapes: interface and type. Understanding their differences helps you write intentional, consistent code.
❌ Inconsistent Use
type UserBase = { id: string; name: string; };
// Mixing interface and type for the same concept:
interface AdminUser {
id: string; name: string; adminLevel: number;
}
interface SuperAdmin extends UserBase { // Mixing idioms
superPowers: string[];
}✅ Consistent Pattern
// Objects and classes: use interface
interface User { id: string; name: string; }
interface AdminUser extends User { adminLevel: number; }
// Unions, intersections, primitives: use type
type UserId = string;
type UserOrAdmin = User | AdminUser;
type WithTimestamps<T> = T & { createdAt: Date; updatedAt: Date };💡
Pro tip: Only interface supports declaration merging — how TypeScript's DOM types work and how libraries extend global types. type cannot be merged.
Paste this code into LearnCodeGuide
Detect TypeScript vulnerabilities and bugs automatically with AI-powered analysis.
Analyze TypeScript Code →