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;
- Type Alias
Container<T>
: The first line defines a type alias calledContainer
. It’s a generic type that takes a type parameterT
. This type alias represents a container that holds a value of typeT
. - Type Alias
NumberContainer
: The second line defines a new type alias calledNumberContainer
. This type alias is essentially a specialization of theContainer
type alias, where the type parameterT
is replaced with the specific typenumber
.
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;
- Type Alias
Nullable<T>
: The first line defines a generic type alias calledNullable
. It takes a type parameterT
, 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]
) ornull
. - Type Alias
NullablePerson
: The second line defines a new type alias calledNullablePerson
. It uses theNullable
type alias to transform the properties of thePerson
type. This effectively creates a new type where each property ofPerson
can now have the original type ornull
.
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;
- Type Alias
Admin
: The first line defines a type alias calledAdmin
. This type alias represents a type for objects that have anisAdmin
property of typeboolean
. In other words, it defines a type for representing administrative users. - Type Alias
AdminPerson
: The second line defines a new type alias calledAdminPerson
. It uses the&
operator to combine thePerson
type with theAdmin
type. This creates a new type that represents an intersection of the properties from both types.