DEV Community

DIWAKARKASHYAP
DIWAKARKASHYAP

Posted on

"type" keyword in Typescript | What is Type Alias ?

Type aliases in TypeScript allow you to create custom names for complex types, making your code more readable and maintainable. They are particularly useful when dealing with complex data structures, union types, and other scenarios where you want to give a clear name to a specific type. Type aliases are created using the type keyword.

Here's a deep explanation of type aliases with examples:

1. Basic Type Alias:
You can create a type alias for a simple data type like this:

type Age = number;
let userAge: Age = 30;
Enter fullscreen mode Exit fullscreen mode

In this example, we've created a type alias called Age, which is essentially a synonym for the number type. It makes the code more self-explanatory when you use Age instead of number.

2. Custom Object Type Alias:
Type aliases are especially handy when defining custom object types:

type Person = {
  name: string;
  age: number;
};

let person: Person = {
  name: "Alice",
  age: 25,
};
Enter fullscreen mode Exit fullscreen mode

Here, we've created a Person type alias that represents the structure of a person object. This makes it clear what properties are expected for a Person without having to repeat the type definition.

3. Union Type Alias:
Type aliases are often used for union types, which allow a variable to have multiple possible types:

type Result = number | string;

let value1: Result = 42;
let value2: Result = "Success";
Enter fullscreen mode Exit fullscreen mode

In this case, Result can be either a number or a string. It's a convenient way to define a custom union type.

4. Function Type Alias:
You can also create aliases for function types:

type Calculator = (a: number, b: number) => number;

const add: Calculator = (x, y) => x + y;
Enter fullscreen mode Exit fullscreen mode

Here, Calculator is a type alias for a function that takes two numbers and returns a number. It provides clarity when dealing with functions in your code.

5. Combining Type Aliases:
You can combine type aliases to create more complex types:

type Point = {
  x: number;
  y: number;
};

type Color = {
  color: string;
};

type ColoredPoint = Point & Color;

let coloredPoint: ColoredPoint = {
  x: 10,
  y: 20,
  color: "red",
};
Enter fullscreen mode Exit fullscreen mode

In this example, we create Point and Color type aliases and then combine them using an intersection (&) to create a ColoredPoint type. This illustrates how type aliases can be used to build up more intricate types.

Type aliases help improve code readability and maintainability by giving meaningful names to complex types and reducing redundancy in your code. They also make your code more self-documenting and make it easier for other developers (or your future self) to understand the intent of your code.

Thank you for reading. I encourage you to follow me on Twitter where I regularly share content about JavaScript and Typescript, as well as contribute to open-source projects and learning Typescript. I am currently seeking a remote job or internship.

Twitter: https://twitter.com/Diwakar_766

GitHub: https://github.com/DIWAKARKASHYAP

Portfolio: https://diwakar-portfolio.vercel.app/

Top comments (0)