When working with TypeScript, one of the most common questions is:
π€ Should I use
typeorinterface?
Both are used to define the structure of data, but they behave differently in certain scenarios.
In this guide, youβll learn:
- What
typeandinterfaceare - Key differences
- All important scenarios with examples
- When to use each (very important)
What is interface in TypeScript?
An interface is used to define the structure (shape) of an object.
Basic Example
interface User {
name: string;
age: number;
}
const user: User = {
name: "Raju",
age: 25,
};
What is type in TypeScript?
A type is more flexible and can define:
- Objects
- Primitives
- Unions
- Tuples
- Functions
Basic Example
type User = {
name: string;
age: number;
};
π Works similar to interface for objects
Key Differences (Quick Overview)
| Feature | interface | type |
|---|---|---|
| Object structure | β Yes | β Yes |
| Union types | β No | β Yes |
| Intersection types | β οΈ Limited | β Yes |
| Declaration merging | β Yes | β No |
| Flexibility | Medium | High |
Scenario 1: Object Definition
π Both work the same
interface User {
name: string;
}
type User = {
name: string;
};
β No major difference here
Scenario 2: Declaration Merging (Important)
β interface supports merging
interface User {
name: string;
}
interface User {
age: number;
}
π Result:
{
name: string;
age: number;
}
β type does NOT support merging
type User = {
name: string;
};
type User = {
age: number;
}; // β Error
Scenario 3: Union Types
β Using type
type Status = "success" | "error" | "loading";
β interface cannot do this
π Not supported
Scenario 4: Intersection Types
β Using type
type Person = {
name: string;
};
type Employee = Person & {
salary: number;
};
β οΈ interface alternative
interface Person {
name: string;
}
interface Employee extends Person {
salary: number;
}
Scenario 5: Functions
β Using type
type Add = (a: number, b: number) => number;
β Using interface
interface Add {
(a: number, b: number): number;
}
Scenario 6: Extending / Inheritance
interface
interface Person {
name: string;
}
interface Employee extends Person {
salary: number;
}
type
type Person = {
name: string;
};
type Employee = Person & {
salary: number;
};
Scenario 7: Primitive Types
β Only type supports this
type ID = string | number;
π interface cannot do this β
Scenario 8: Tuples
type Point = [number, number];
π Only type supports tuples
Scenario 9: Working with Classes
interface (preferred)
interface User {
name: string;
}
class Person implements User {
name = "Raju";
}
When to Use interface
Use interface when:
- Defining object structure
- Working with classes
- Need declaration merging
- Building large applications
When to Use type
Use type when:
- Using unions (
|) - Using intersections (
&) - Working with primitives
- Advanced type logic
Best Practice (Very Important)
π Use both together:
type Status = "success" | "error";
interface ApiResponse {
status: Status;
data: string;
}
Common Mistakes
β Using interface for unions
interface Status = "success" | "error"; // β
β Overusing any
π Avoid losing type safety
β Confusion between both
π Use based on use case
Interview Tip
If asked:
βtype vs interface?β
π Answer:
βBoth define data shapes, but type is more flexible (supports unions, tuples), while interface supports declaration merging and is better for object-oriented design.β
Final Summary
- Both define structure of data
interfaceβ best for objects & classestypeβ best for advanced types- Use both based on need
π‘ Found this helpful? Subscribe to get simple TypeScript tutorials, interview questions, and real-world coding tips. Happy Coding!