A type alias allows us to create a custom name for a type. Type aliases are useful for giving meaningful names to objects, functions, unions, arrays, and other TypeScript types.
type keyword.
They make complex types easier to reuse and understand.
A type alias gives another name to an existing type.
type StudentName = string;
let name: StudentName = "Amit";
Here, StudentName is an alias for
the string type.
The basic syntax is:
type TypeName = Type;
Example:
type Age = number;
let studentAge: Age = 20;
A type alias can represent a string type.
type Name = string;
let studentName: Name = "Rahul";
console.log(studentName);
This makes the purpose of the value clearer.
We can create an alias for the number type.
type StudentAge = number;
let age: StudentAge = 21;
console.log(age);
A boolean type can also have a custom name.
type ActiveStatus = boolean;
let active: ActiveStatus = true;
console.log(active);
One of the most common uses of type aliases is defining object structures.
type Student = {
name: string;
age: number;
};
let student: Student = {
name: "Amit",
age: 20
};
Once an object type alias is created, it can be used for multiple objects.
type Student = {
name: string;
age: number;
};
const student1: Student = {
name: "Amit",
age: 20
};
const student2: Student = {
name: "Rahul",
age: 21
};
A type alias can describe an object with many properties.
type Student = {
id: number;
name: string;
age: number;
course: string;
};
const student: Student = {
id: 101,
name: "Amit",
age: 20,
course: "TypeScript"
};
A type alias can represent an array type.
type Numbers = number[];
const marks: Numbers = [
80,
75,
90
];
console.log(marks);
We can create an alias for an array of strings.
type Skills = string[];
const skills: Skills = [
"HTML",
"CSS",
"TypeScript"
];
console.log(skills);
A type alias can represent a union of multiple types.
type ID = number | string;
let studentId: ID = 101;
studentId = "ST101";
The ID type can contain either a number or a string.
A type alias can restrict a value to specific literal values.
type Direction =
"left" |
"right" |
"up" |
"down";
let direction: Direction = "left";
Only the specified values can be assigned.
Object type aliases can contain optional properties.
type User = {
name: string;
email: string;
phone?: string;
};
const user: User = {
name: "Amit",
email: "amit@example.com"
};
The phone property is optional.
A type alias can contain readonly properties.
type Student = {
readonly id: number;
name: string;
};
const student: Student = {
id: 101,
name: "Amit"
};
student.name = "Rahul";
// student.id = 102;
A readonly property cannot be reassigned after initialization.
Type aliases can describe function types.
type AddFunction = (
a: number,
b: number
) => number;
const add: AddFunction = (
a,
b
) => a + b;
console.log(
add(10, 20)
);
The same function type can be used by different functions.
type Operation = (
a: number,
b: number
) => number;
const add: Operation = (
a,
b
) => a + b;
const multiply: Operation = (
a,
b
) => a * b;
console.log(
add(10, 20)
);
console.log(
multiply(10, 20)
);
Type aliases can describe nested object structures.
type Address = {
city: string;
pin: number;
};
type Student = {
name: string;
address: Address;
};
const student: Student = {
name: "Amit",
address: {
city: "Aurangabad",
pin: 824101
}
};
A type alias can combine objects and arrays.
type Student = {
name: string;
marks: number[];
};
const student: Student = {
name: "Rahul",
marks: [
80,
75,
90
]
};
A type alias can describe an array containing objects.
type Student = {
name: string;
age: number;
};
type Students = Student[];
const students: Students = [
{
name: "Amit",
age: 20
},
{
name: "Rahul",
age: 21
}
];
Type aliases can make function parameters easier to read.
type Student = {
name: string;
age: number;
};
function showStudent(
student: Student
): void {
console.log(
student.name
);
console.log(
student.age
);
}
showStudent({
name: "Amit",
age: 20
});
A function can use a type alias as its return type.
type Student = {
name: string;
age: number;
};
function getStudent(): Student {
return {
name: "Amit",
age: 20
};
}
console.log(
getStudent()
);
Type aliases are commonly used to define React component data and props.
type User = {
name: string;
age: number;
};
const user: User = {
name: "Amit",
age: 20
};
The same type can later be used for component props.
A type alias can describe the props of a React component.
type UserProps = {
name: string;
age: number;
};
function UserCard(
props: UserProps
) {
return (
<div>
{props.name}
{props.age}
</div>
);
}
This keeps component data structured and type-safe.
Type aliases are very useful in React Native for defining component props and application data.
type User = {
id: number;
name: string;
email: string;
};
const user: User = {
id: 101,
name: "Rahul",
email: "rahul@example.com"
};
Type aliases can combine multiple types using
the intersection operator &.
type Person = {
name: string;
};
type Employee = {
salary: number;
};
type Worker = Person & Employee;
const worker: Worker = {
name: "Amit",
salary: 30000
};
Type aliases are especially useful for giving names to union types.
type Status =
"loading" |
"success" |
"error";
let status: Status = "loading";
status = "success";
This prevents unrelated values from being assigned.
A common mistake is confusing a type alias with a variable declaration.
// Correct
type Student = {
name: string;
age: number;
};
const student: Student = {
name: "Amit",
age: 20
};
The type keyword creates a type,
while const or let creates a variable.
Type aliases are useful when:
Here is a practical student management example using multiple type aliases.
type Address = {
city: string;
pin: number;
};
type Student = {
id: number;
name: string;
age: number;
course: string;
marks: number[];
address: Address;
};
const student: Student = {
id: 101,
name: "Amit",
age: 20,
course: "TypeScript",
marks: [
80,
75,
90
],
address: {
city: "Aurangabad",
pin: 824101
}
};
console.log(
student.name
);
console.log(
student.address.city
);
console.log(
student.marks
);
Type aliases allow us to create meaningful names for TypeScript types.
type Student = {
name: string;
age: number;
course: string;
};
const student: Student = {
name: "Amit",
age: 20,
course: "TypeScript"
};
They make complex types reusable, readable, and easier to maintain.
type keyword.Question: Which keyword is used to create a type alias in TypeScript?