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 AliasesAliasName: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 TypeAlias 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: 0Defining 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 101The ID is A102Best Practices for Using TypeScript Type AliasesUse 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. Comment More infoAdvertise with us Next Article TypeScript Aliases Type A akshitsaxenaa09 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More 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.Syntaxlet variableName: As 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.Prim 3 min read Like