Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
TypeScript any Type
Next article icon

TypeScript Aliases Type

Last Updated : 22 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In TypeScript, a type alias allows you to assign a custom name to an existing type, enhancing code readability and reusability.

  • Provide a shorthand for complex types like unions or objects.
  • Allow naming of primitive types, object types, or functions for clarity.
  • Simplify repetitive type definitions and improve maintainability.
JavaScript
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 }); 
  • 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.

Output:

Drawing a circle at (10, 20)

Parameters of Type Aliases

  • AliasName:
    • This is the name you assign to the type alias. It must be a valid TypeScript identifier.
    • Example: Point, Shape, UserProfile.
  • ExistingType:
    • This refers to the actual data type or structure the alias represents.
    • Example: string, number, { x: number; y: number; }.

More Examples of TypeScript aliases Type

Alias for a Union Type

JavaScript
type ID = number | string;  let userId: ID; userId = 101;       // Valid assignment userId = "A123";    // Also valid 
  • 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.

Output:

Origin: { x: 0, y: 0 }
Distance from Origin: 0

Defining a User Profile with Type Aliases

JavaScript
type UserProfile = {     username: string;     email: string;     age: number; };  const user: UserProfile = {     username: "Akshit Saxena",     email: "[email protected]",     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)); 
  • 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.

Output:

Hello, Akshit Saxena! 
You are 24 years old.
Your email is [email protected].

Using Type Aliases for Union Types

JavaScript
type ID = number | string;  function displayId(id: ID): void {     console.log(`The ID is ${id}`); }  displayId(101); displayId("A102"); 
  • ID is a type alias representing a union of number and string.
  • The displayId function accepts an ID, allowing for flexible input types.

Output:

The ID is 101
The ID is A102

Best Practices for Using TypeScript Type Aliases

  • Use Descriptive Names: Choose clear and meaningful names for type aliases to enhance code readability.
  • Keep Types Focused: Define type aliases for specific, well-defined structures to maintain clarity.
  • Document Complex Types: Provide comments or documentation for complex type aliases to aid understanding.

Next Article
TypeScript any Type

A

akshitsaxenaa09
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • TypeScript
  • Geeks Premier League 2023

Similar Reads

  • TypeScript Assertions Type
    TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient. Syntax let variableName:
    2 min read
  • TypeScript Interfaces Type
    TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:in
    2 min read
  • TypeScript any Type
    In TypeScript, any type is a dynamic type that can represent values of any data type. It allows for flexible typing but sacrifices type safety, as it lacks compile-time type checking, making it less recommended in strongly typed TypeScript code. It allows developers to specify types for variables, f
    4 min read
  • Opaque Types In TypeScript
    In TypeScript Opaque types concept allows for the definition of specialized types such as strings or numbers which are derived from primitive types but do not have the characteristics of the base types, the purpose of this is to prevent specific actions regarding the type in question, or to make thi
    4 min read
  • Data types in TypeScript
    In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity. Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Pri
    3 min read
  • TypeScript Functions Type
    TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage. Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime erro
    6 min read
  • TypeScript Object Types
    TypeScript object types define the structure of objects by specifying property types, ensuring type safety and clarity when passing objects as function parameters. Optional properties, denoted with a ? provide flexibility for objects with varying properties. This approach enhances code robustness by
    3 min read
  • Typescript Generic Type Array
    A generic type array is an array that is defined using the generic type in TypeScript. A generic type can be defined between the angled brackets(<>). Syntax:// Syntax for generic type arraylet myArray: Array<Type>;Example 1: Creating a simple generic type array of number type in Typescri
    1 min read
  • TypeScript Generic Classes
    Generics in TypeScript allow us to create reusable and type-safe components. Generic classes help in defining a blueprint that can work with different data types without sacrificing type safety. They enable better code reusability and flexibility by allowing us to define type parameters that will be
    5 min read
  • TypeScript Generic Types
    TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences