VOOZH about

URL: https://www.geeksforgeeks.org/typescript/typescript-aliases-type/

⇱ TypeScript Aliases Type - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TypeScript Aliases Type

Last Updated : 10 Sep, 2025

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:

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.

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.

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.

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.


Comment
Article Tags:

Explore