Welcome back! I am Mihir, and in this lesson we will compare interface vs type in TypeScript.
Both can describe object shapes, but they are not exactly the same.
Similarity: Both Can Describe Objects
Using interface:
interface User {
id: number;
name: string;
}Using type:
type User = {
id: number;
name: string;
};For simple object shapes, both work well.
Interfaces Can Extend Interfaces
interface Person {
name: string;
}
interface Employee extends Person {
employeeId: number;
}Employee includes all properties from Person.
Types Can Use Intersections
type Person = {
name: string;
};
type Employee = Person & {
employeeId: number;
};This gives a similar result using &.
Types Can Represent Unions
This is where type is often better.
type Status = "pending" | "success" | "failed";An interface cannot directly represent this kind of union.
You cannot write:
interface Status = "pending" | "success";So for unions, use type.
Types Can Name Primitives
type UserId = number;
type Email = string;Interfaces are for object-like structures, not primitive aliases.
Interfaces Can Merge
Interfaces with the same name are merged.
interface Config {
theme: string;
}
interface Config {
language: string;
}
const config: Config = {
theme: "dark",
language: "en",
};This can be useful when extending library types.
Type Aliases Cannot Merge
type Config = {
theme: string;
};
type Config = {
language: string;
};This causes an error because a type alias name can only be declared once in the same scope.
Which One Should You Use?
Use interface when:
- you are describing object shapes
- you expect extension
- you are working with class contracts
- you want declaration merging
Use type when:
- you need unions
- you need primitive aliases
- you need tuple aliases
- you are composing complex types
Practical Rule
For beginners, this rule works well:
// Object shape
interface User {
id: number;
name: string;
}
// Union or helper type
type Status = "loading" | "success" | "error";This keeps your code predictable.
Quick Recap
interfaceandtypecan both describe objects.typeis better for unions, primitives, tuples, and complex compositions.interfacesupports declaration merging.- There is no single winner; choose based on the job.
Next up, we will learn Extending Interfaces → in more detail.