TypeScript Aliases Type
A type alias is a way to give a name to a type, allowing complex type definitions to be simplified and reused. It can be used with primitive types, objects, arrays, unions, intersections, tuples, or function types.
Syntax:
type AliasName = Type;
In the above syntax:
- type: keyword to declare a type alias.
- AliasName: the custom name for the type.
- Type: any valid TypeScript type: primitive, object, array, union, intersection, tuple, or function.
Now let's understand this with the help of example:
type Point = {
x: number;
y: number;
};
type Shape = "circle" | "square" | "rectangle";
function drawShape(shape: Shape, position: Point): void {
console.log(`Drawing a ${shape} at (${position.x}, ${position.y})`);
}
drawShape("circle", { x: 10, y: 20 });
Output:
Drawing a circle at (10, 20)
In this example:
- Point is a type alias for an object with x and y as number.
- Shape is a type alias for a union of specific string literals.
- The drawShape function accepts a Shape and a Point, ensuring strong type safety and clarity.
Alias for a Union Type
A union type allows a variable to hold values of multiple types. Using a type alias for a union makes the code cleaner and easier to maintain.
type ID = number | string;
let userId: ID;
userId = 101; // Valid assignment
userId = "A123"; // Also valid
Output:
Origin: { x: 0, y: 0 }
Distance from Origin: 0
In this example:
- ID is a type alias that allows a variable to be either a number or a string.
- This provides flexibility for userId to accept both numeric and alphanumeric identifiers.
Defining a User Profile with Type Aliases
A type alias can be used to define the structure of a user profile, making it easy to manage and reuse throughout the code. This is especially useful for objects with multiple properties.
type UserProfile = {
username: string;
email: string;
age: number;
};
const user: UserProfile = {
username: "Akshit Saxena",
email: "akshit.saxena@geeksforgeeks.com",
age: 24,
};
function greetUser(profile: UserProfile): string {
return `Hello, ${profile.username}!
You are ${profile.age} years old.
Your email is ${profile.email}.`;
}
console.log(greetUser(user));
Output:
Hello, Akshit Saxena!
You are 24 years old.
Your email is akshit.saxena@geeksforgeeks.com.
In this example:
- UserProfile is a type alias for an object with username, email, and age properties.
- The greetUser function utilizes this alias to ensure it receives a properly structured user profile.
Using Type Aliases for Union Types
A union type allows a variable to hold values of multiple types. Type aliases can simplify union types by giving them a custom name, improving readability and reusability.
type ID = number | string;
function displayId(id: ID): void {
console.log(`The ID is ${id}`);
}
displayId(101);
displayId("A102");
Output:
The ID is 101
The ID is A102
In this example:
- ID is a type alias representing a union of number and string.
- The displayId function accepts an ID, allowing for flexible input types.