![]() |
VOOZH | about |
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:
Now let's understand this with the help of example:
Output:
Drawing a circle at (10, 20)In this example:
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.
Output:
Origin: { x: 0, y: 0 }
Distance from Origin: 0In this example:
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.
Output:
Hello, Akshit Saxena!
You are 24 years old.
Your email is akshit.saxena@geeksforgeeks.com.In this example:
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.
Output:
The ID is 101
The ID is A102In this example: