Types & InterfacesBeginner
1Question 1 of 1
typescriptWhat is the difference between Types and Interfaces in TypeScript?
Explanation & Concept
Both `type` and `interface` define object contracts, but they differ in capabilities:
• Interfaces: Can be declared multiple times to merge declarations (declaration merging). Ideal for public library APIs and standard object contracts. • Type Aliases: More expressive. Can represent union types (`type ID = string | number`), primitives, tuples, and mapped types. Cannot be re-opened for merging.
Code Example
typescript
// Interface declaration merging
interface User {
id: string;
name: string;
}
interface User {
role: "admin" | "member"; // Automatically merged!
}
// Type alias with union & intersection
type Status = "pending" | "approved" | "rejected";
type UserWithStatus = User & { status: Status };
const activeUser: UserWithStatus = {
id: "u_1",
name: "Jordan",
role: "admin",
status: "approved"
};How It Works & Key Takeaways
Use `interface` for object models and public API definitions. Use `type` when union types, tuples, or complex conditional transformations are required.
Mock Test · 1 Questions · Optional
Ready for Generics?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner2 questions