Series: TypeScript from Basics to Advanced
Part: 5 — Type Aliases & Interfaces
Level: Beginner → Intermediate
Introduction
In the previous article, we learned how to work with arrays and objects in TypeScript.
We created structures like:
type User = {
id: number;
name: string;
email: string;
};
But imagine that your application has hundreds of files and you need to use the User structure in many places.
You don't want to repeatedly write:
{
id: number;
name: string;
email: string;
}
Instead, TypeScript allows us to create reusable type definitions.
The two most common ways to do this are:
type
interface
For example:
type User = {
id: number;
name: string;
};
or:
interface User {
id: number;
name: string;
}
Both describe the shape of an object.
So an obvious question is:
What's the difference between
typeandinterface?
Let's understand both before comparing them.
1. What Is a Type Alias?
A type alias allows us to give a name to a type.
For example:
type User = {
id: number;
name: string;
email: string;
};
Now we can use User anywhere:
const user: User = {
id: 1,
name: "Sundar",
email: "sundar@example.com"
};
We can also use it in functions:
function printUser(user: User): void {
console.log(user.name);
}
This makes our code reusable and easier to maintain.
2. Why Do We Need Type Aliases?
Without a type alias, we might repeatedly write:
function printUser(
user: {
id: number;
name: string;
email: string;
}
) {
console.log(user.name);
}
This becomes difficult to maintain.
Instead:
type User = {
id: number;
name: string;
email: string;
};
function printUser(user: User) {
console.log(user.name);
}
Now the structure has a single source of truth.
If we add a property later:
type User = {
id: number;
name: string;
email: string;
age: number;
};
TypeScript can identify places where the updated type needs to be handled.
3. Type Aliases Aren't Limited to Objects
One important feature of type is that it can represent many different kinds of types.
For example:
type UserId = string;
Now:
const userId: UserId = "USR_001";
We can also create a union:
type Status = "loading" | "success" | "error";
Now:
let status: Status = "loading";
This is valid:
status = "success";
But this isn't:
status = "completed";
because "completed" isn't part of the Status type.
4. Type Aliases for Arrays
We can create aliases for arrays too.
type User = {
id: number;
name: string;
};
type Users = User[];
Now:
const users: Users = [
{
id: 1,
name: "Sundar"
},
{
id: 2,
name: "Rahul"
}
];
Although User[] is often simpler, this demonstrates that a type alias can represent many kinds of types.
5. Type Aliases for Functions
Type aliases can also describe functions.
type Calculator = (
a: number,
b: number
) => number;
Now:
const add: Calculator = (a, b) => {
return a + b;
};
And:
const multiply: Calculator = (a, b) => {
return a * b;
};
This is particularly useful when the same function signature is used in multiple places.
6. Optional Properties
We can mark properties as optional using ?.
type User = {
id: number;
name: string;
email?: string;
};
Now both are valid:
const user1: User = {
id: 1,
name: "Sundar"
};
And:
const user2: User = {
id: 2,
name: "Rahul",
email: "rahul@example.com"
};
The email property is optional.
7. Readonly Properties
We can also make properties readonly.
type User = {
readonly id: number;
name: string;
};
Now:
const user: User = {
id: 1,
name: "Sundar"
};
This is allowed:
user.name = "Rahul";
But this isn't:
user.id = 2;
Because id is readonly.
Readonly is useful for values that should not be modified after creation.
8. What Is an Interface?
An interface describes the structure of an object.
For example:
interface User {
id: number;
name: string;
email: string;
}
We can use it exactly like our type alias:
const user: User = {
id: 1,
name: "Sundar",
email: "sundar@example.com"
};
We can also use it with functions:
function printUser(user: User): void {
console.log(user.name);
}
So far, type and interface look almost identical.
That's because for many object-shape use cases, they are.
The differences become more interesting when we look at extension and composition.
9. Extending Interfaces
Interfaces can extend other interfaces.
For example:
interface User {
id: number;
name: string;
}
We can create:
interface Admin extends User {
permissions: string[];
}
Now Admin contains:
id
name
permissions
Example:
const admin: Admin = {
id: 1,
name: "Sundar",
permissions: [
"create",
"update",
"delete"
]
};
This is a common way to build related object structures.
10. Extending Multiple Interfaces
An interface can extend multiple interfaces.
interface Person {
name: string;
}
interface Employee {
employeeId: number;
}
interface Developer extends Person, Employee {
skills: string[];
}
Now:
const developer: Developer = {
name: "Sundar",
employeeId: 101,
skills: [
"JavaScript",
"TypeScript",
"React"
]
};
This allows us to compose interfaces from smaller pieces.
11. Interface Declaration Merging
One interesting feature of interfaces is declaration merging.
Suppose we write:
interface User {
name: string;
}
And later:
interface User {
age: number;
}
TypeScript combines them.
The resulting interface behaves like:
interface User {
name: string;
age: number;
}
Therefore:
const user: User = {
name: "Sundar",
age: 25
};
This behavior is called declaration merging.
It can be useful when extending existing library types.
However, you generally shouldn't rely on declaration merging for normal application models unless you have a specific reason.
12. Type Intersections
Types can be combined using the intersection operator:
&
For example:
type Person = {
name: string;
};
type Employee = {
employeeId: number;
};
We can create:
type Developer = Person & Employee;
Now Developer must contain both:
name
employeeId
Example:
const developer: Developer = {
name: "Sundar",
employeeId: 101
};
This is similar to:
interface Developer extends Person, Employee {}
Both approaches can express composition.
13. Type Unions
Another important feature of type aliases is union types.
A union means a value can be one of several types.
type ID = string | number;
Now:
let id: ID;
id = "USR_001";
id = 100;
Both are valid.
But:
id = true;
is invalid.
Interfaces cannot directly represent this kind of union:
type ID = string | number;
This is one of the major reasons type is extremely useful.
14. Literal Types
Type aliases can also represent specific values.
For example:
type Role = "admin" | "user" | "guest";
Now:
let role: Role = "admin";
Valid:
role = "user";
role = "guest";
Invalid:
role = "manager";
This pattern is very common in modern TypeScript applications.
For example:
type RequestStatus =
| "idle"
| "loading"
| "success"
| "error";
15. Type vs Interface
The important point is that neither is universally "better."
They have different strengths.
16. type vs interface in React
Both are commonly used in React projects.
For example:
type ButtonProps = {
label: string;
disabled?: boolean;
};
Or:
interface ButtonProps {
label: string;
disabled?: boolean;
}
Both work.
Then:
function Button({
label,
disabled
}: ButtonProps) {
return (
<button disabled={disabled}>
{label}
</button>
);
}
The choice often comes down to team conventions and the kind of type you're describing.
17. When Should You Use interface?
Interfaces are particularly natural for object-shaped contracts.
For example:
interface User {
id: number;
name: string;
email: string;
}
They are also useful when you expect the structure to be extended:
interface Admin extends User {
permissions: string[];
}
Interfaces are especially common when defining public object contracts or library APIs.
18. When Should You Use type?
Use type when you need features such as:
Union types
type Status = "success" | "error";
Intersection types
type Developer = Person & Employee;
Function types
type Callback = (value: string) => void;
Tuples
type Coordinates = [number, number];
More complex type composition
type Response<T> =
| {
status: "success";
data: T;
}
| {
status: "error";
message: string;
};
This is one reason many TypeScript developers prefer type as a general-purpose type composition tool.
19. A Practical Example
Imagine we're building an online test application.
We could define:
interface User {
id: string;
name: string;
}
A question:
interface Question {
id: string;
question: string;
options: string[];
}
A test:
interface Test {
id: string;
title: string;
questions: Question[];
}
Then we could use a type alias for status:
type TestStatus =
| "not-started"
| "in-progress"
| "completed";
And an API response:
type ApiResponse<T> = {
status: "success" | "error";
message: string;
data: T;
};
Now:
type TestResponse = ApiResponse<Test>;
This combination is very common in real-world TypeScript applications.
20. A Common Pattern: Interface + Type
You don't have to choose one exclusively.
A project can use both.
For example:
interface User {
id: string;
name: string;
email: string;
}
Then:
type UserRole = "admin" | "user";
And:
type AuthenticatedUser = User & {
role: UserRole;
};
This is perfectly valid.
The goal isn't to use only type or only interface.
The goal is to choose the construct that best expresses the model.
21. A Practical Rule of Thumb
If you're unsure what to use, this is a reasonable starting point:
Use interface for object contracts
interface User {
id: number;
name: string;
}
Use type for unions and type composition
type Status = "loading" | "success" | "error";
type UserWithRole = User & {
role: string;
};
But don't treat this as a strict law.
Modern TypeScript supports both approaches very well.
22. Common Mistakes
Mistake 1: Thinking interface is always better
There is no rule that says interfaces are always better than type aliases.
Both have legitimate uses.
Mistake 2: Using interface for unions
This doesn't work:
interface Status {
"loading" | "success" | "error";
}
Use:
type Status =
| "loading"
| "success"
| "error";
Mistake 3: Repeating Object Structures
Avoid:
function getUser(user: {
id: number;
name: string;
}) {}
function updateUser(user: {
id: number;
name: string;
}) {}
Instead:
interface User {
id: number;
name: string;
}
Then:
function getUser(user: User) {}
function updateUser(user: User) {}
Mistake 4: Creating Types That Are Too Generic
Avoid:
type Data = {
value: any;
};
This doesn't provide much type safety.
Prefer a meaningful model:
type User = {
id: string;
name: string;
};
23. The Bigger Picture
So far, our TypeScript journey looks like this:
Basic Types
↓
Functions
↓
Arrays & Objects
↓
Type Aliases & Interfaces
We're gradually moving from individual values to complete application models.
For example:
interface User {
id: string;
name: string;
}
type UserRole = "admin" | "user";
type AuthenticatedUser =
User & {
role: UserRole;
};
This is the foundation for more advanced concepts such as:
Union Types
Intersection Types
Type Narrowing
Generics
Utility Types
Conditional Types
Mapped Types
Type vs Interface Cheat Sheet
type
Use when you need:
Union
Intersection
Tuple
Function type
Literal type
Complex type composition
Example:
type Status = "loading" | "success" | "error";
interface
Use when you need:
Object structure
Extending object structures
Declaration merging
Public object contracts
Example:
interface User {
id: number;
name: string;
}
Final Takeaway
Type aliases and interfaces allow us to create reusable descriptions of our application's data.
The simplest examples are:
type User = {
id: number;
name: string;
};
and:
interface User {
id: number;
name: string;
}
For basic object structures, both can work well.
The biggest practical distinction is that type can represent things beyond object shapes:
type Status = "loading" | "success" | "error";
type ID = string | number;
type Calculator = (
a: number,
b: number
) => number;
while interfaces are particularly useful for extensible object contracts:
interface User {
id: number;
name: string;
}
interface Admin extends User {
permissions: string[];
}
A good TypeScript developer doesn't ask:
"Which one is always better?"
Instead, ask:
"Which one best expresses the type I'm trying to model?"