
Published on:
Types vs Interfaces in TypeScript: What You Need to Know
If you're diving into TypeScript, one of the first things you'll encounter is the choice between type
and interface
. At first glance, they seem interchangeable — both allow you to describe the shape of objects, function signatures, and more. But dig deeper, and you’ll find subtle yet important differences that can impact how you write and maintain your code.
Let’s break down what you really need to know about type
vs interface
in TypeScript.
The Similarities
Both type
and interface
can be used to:
- Describe the shape of an object:
type User = {
name: string;
age: number;
};
interface User {
name: string;
age: number;
}
- Add optional or readonly properties:
type Product = {
name: string;
readonly price: number;
description?: string;
};
- Describe function signatures:
type Log = (message: string) => void;
interface Log {
(message: string): void;
}
Key Differences
1. Extending and Implementing
interface
was made for extension and is ideal for object-oriented designs.
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
type
can also extend other types using intersections:
type Animal = {
name: string;
};
type Dog = Animal & {
breed: string;
};
- Classes can only
implement
interfaces (not types):
class Bulldog implements Dog {
name = "Max";
breed = "Bulldog";
}
✅ Use
interface
when you expect your structure to be extended or implemented by classes.
2. Union and Intersection Types
type
shines when you need to define union or intersection types:
type ID = string | number;
type ApiResponse = SuccessResponse | ErrorResponse;
interface
can’t do this — it’s only for object-like structures.
✅ Use
type
when you're combining multiple types or creating unions.
3. Declaration Merging
- One unique feature of
interface
is declaration merging — if you define an interface twice, TypeScript merges them:
interface Car {
make: string;
}
interface Car {
model: string;
}
// Car now has both `make` and `model`
const myCar: Car = {
make: "Toyota",
model: "Corolla",
};
type
doesn’t support this. Redeclaring atype
will cause an error.
✅ Use
interface
if you want to allow augmentation (e.g., extending third-party types).
4. Performance
For large codebases, interface
is generally more performant for the TypeScript compiler. While the difference is often negligible, it can matter in very large-scale projects.
✅ Use
interface
in performance-critical or library-level code.
So... Which One Should You Use?
Here’s a simple rule of thumb:
- Use
interface
when you’re working with object shapes, especially if they’re going to be extended, implemented, or merged. - Use
type
when you need advanced features like unions, intersections, or aliasing primitive types.
That said, consistency is more important than the choice itself. Pick one that fits your style and team’s needs, and stick to it.
Conclusion
In modern TypeScript, you can often use either type
or interface
and be just fine. But understanding their nuances helps you write cleaner, more scalable code — and helps your future self (and teammates) a whole lot.
Whether you're building libraries, APIs, or frontends, knowing when and why to use each can be the difference between smooth sailing and head-scratching errors.
Happy typing! 👨💻👩💻