Get a Quote Right Now

Edit Template

Type Aliase

Type aliases in TypeScript allow you to create custom names for existing types, making your code more readable and expressive. They are particularly useful for complex type annotations, unions, intersections, and for creating more meaningful names for combinations of types.

Basic Type Alias:
You can create a type alias using the type keyword. This is useful for simplifying complex type expressions.
type Age = number;
type Person = { name: string; age: Age };


Union and Intersection Types: Type aliases can be used to create union and intersection types, combining multiple types into one.
type StringOrNumber = string | number;
type Point = { x: number; y: number };
type Point3D = Point & { z: number };


Function Signatures: Type aliases can also define function signatures.
type MathOperation = (a: number, b: number) => number;

Generics with Type Aliases: Type aliases can include generic parameters, making them more versatile.
type Container = { value: T };
type NumberContainer = Container;

  1. Type Alias Container<T>: The first line defines a type alias called Container. It’s a generic type that takes a type parameter T. This type alias represents a container that holds a value of type T.
  2. Type Alias NumberContainer: The second line defines a new type alias called NumberContainer. This type alias is essentially a specialization of the Container type alias, where the type parameter T is replaced with the specific type number.

In simpler terms, NumberContainer is a type alias for a container that holds a value of type number.


Type Aliases for Complex Structures: You can use type aliases to define complex and nested structures.
type User = {
id: number;
name: string;
address: {
street: string;
city: string;
};
};


Mapped Types with Type Aliases: Mapped types can be created using type aliases to transform properties of existing types.

type Nullable = { [K in keyof T]: T[K] | null };
type NullablePerson = Nullable;

  1. Type Alias Nullable<T>: The first line defines a generic type alias called Nullable. It takes a type parameter T, which represents the original type you want to modify. This type alias creates a new type by mapping each property of the original type to a property in the new type where the value can be either the original value’s type (T[K]) or null.
  2. Type Alias NullablePerson: The second line defines a new type alias called NullablePerson. It uses the Nullable type alias to transform the properties of the Person type. This effectively creates a new type where each property of Person can now have the original type or null.
    NOTE:- <T> acts as a sort of place holder.

Intersection of Types: Type aliases can be used to create intersections of types, combining multiple types into one.

type Admin = { isAdmin: boolean };
type AdminPerson = Person & Admin;

  1. Type Alias Admin: The first line defines a type alias called Admin. This type alias represents a type for objects that have an isAdmin property of type boolean. In other words, it defines a type for representing administrative users.
  2. Type Alias AdminPerson: The second line defines a new type alias called AdminPerson. It uses the & operator to combine the Person type with the Admin type. This creates a new type that represents an intersection of the properties from both types.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *