Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 9
Type Aliases vs Interfaces
TypeScript provides two ways to name a type: `type` aliases and `interface` declarations. They are similar for objects but differ in key ways. Knowing when to use each - and being consistent within a codebase - is a mark of TypeScript maturity.
Type Aliases vs Interfaces - Full Comparison
// ── Interface - open, extendable, for object shapes ──────────
interface User {
id: number;
name: string;
email: string;
}
// Extending an interface
interface AdminUser extends User {
role: "admin" | "superadmin";
permissions: string[];
}
// Declaration merging - interfaces can be re-opened (augmentation)
// Useful for extending third-party types:
interface Window {
myAnalytics: () => void; // adds to built-in Window interface
}
// ── Type alias - closed, flexible, for any type ────────────────
type UserId = number; // primitive alias
type Status = "active" | "inactive"; // union
type Pair<T> = [T, T]; // generic tuple
type NullableString = string | null; // nullable
type UserOrAdmin = User | AdminUser; // union of objects
// Type alias extending: use intersection (&) not extends
type SuperAdmin = AdminUser & {
createdAt: Date;
superPowers: string[];
};
// ── Which to use? ─────────────────────────────────────────────
// Interface → when defining the shape of objects, classes, or APIs
// that may need to be extended or merged later
// Type alias → for unions, intersections, primitives, tuples,
// or utility type compositions
// ── Practical examples ────────────────────────────────────────
// ✅ Interface for domain model (can be implemented by a class)
interface TaskRepository {
findById(id: number): Promise<Task | null>;
save(task: Task): Promise<Task>;
delete(id: number): Promise<void>;
}
// ✅ Type alias for union (interfaces cannot represent unions cleanly)
type ApiResult<T> =
| { success: true; data: T }
| { success: false; error: string };
// ✅ Type alias for utility type composition
type CreateTaskInput = Omit<Task, "id" | "createdAt" | "updatedAt">;
// ── What interfaces CANNOT do ─────────────────────────────────
// ❌ interfaces cannot represent union types:
// interface StringOrNumber = string | number; // SYNTAX ERROR
// ── What type aliases CANNOT do ──────────────────────────────
// ❌ type aliases cannot be re-opened for declaration mergingCommon Mistakes
- Using `type` for everything out of habit - interfaces should be the default for object shapes that may be extended or implemented by classes.
- Using `interface` for union types - interfaces only describe object shapes. `type Status = 'active' | 'inactive'` must be a `type`.
- Mixing conventions within a codebase - pick one as the default for domain objects (`interface`) and stick with it. Use `type` selectively for composites.
Tip
Tip
Practice Type Aliases vs Interfaces in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Type Aliases vs Interfaces
Practice Task
Note
Practice Task - (1) Write a working example of Type Aliases vs Interfaces from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Type Aliases vs Interfaces is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- TypeScript provides two ways to name a type: `type` aliases and `interface` declarations.
- Using `type` for everything out of habit - interfaces should be the default for object shapes that may be extended or implemented by classes.
- Using `interface` for union types - interfaces only describe object shapes. `type Status = 'active' | 'inactive'` must be a `type`.
- Mixing conventions within a codebase - pick one as the default for domain objects (`interface`) and stick with it. Use `type` selectively for composites.
Finished this lesson?