Typescript utility types are a set of built-in type utilities that provide common type transformations. They are designed to help with working with existing types and easily constructing new types. These utility types include Pick, Omit, Exclude, Record, Partial, Readonly, and more. They can be used to create custom types for use in a project.
Utility types are a powerful tool that allow you to create custom types from existing types. They are especially useful when working with complex data structures. For example, you can use Pick to select only certain properties from an existing type, Omit to exclude certain properties from an existing type, Exclude to create a type that excludes certain specific types, Record to create a type of a specific set of keys and corresponding values, Partial to create a type that contains only some of the properties of an existing type, Readonly to make all properties of a type read-only, and more.
Here's a list of all the common utility types that we can utilize for type transformations, note that the utility types are not limited to just the ones listed here.
- Pick
- Omit
- Exclude
- Record
- Partial
- Readonly
- Constructor
- NonNullable
- Required
In no particular order let's start with Pick
Pick
The Pick utility type is used to select only certain properties from an existing type. For example, if you have an interface with multiple properties, you can use Pick to create a new type with only the properties you want.
For example, suppose you have an interface called Person that has the following properties:
interface Person {
name: string;
age: number;
height: number;
weight: number;
}
You can use Pick to create a new type that only includes the name and age properties:
type PersonInfo = Pick<Person, 'name' | 'age'>;
This creates a new type called PersonInfo that only includes the name and age properties. The Pick utility type should be used when you need to create a new type from an existing type that contains only certain properties. It can be used to create custom types for use in a project, or to simplify existing types for use in other operations.
Omit
The Omit utility type is used to exclude certain properties from an existing type. For example, if you have an interface with multiple properties, you can use Omit to create a new type without the properties you don't want.
For example, suppose you have an interface called Person that has the following properties:
interface Person {
name: string;
age: number;
height: number;
weight: number;
}
You can use Omit to create a new type that excludes the height and weight properties:
type PersonInfo = Omit<Person, 'height' | 'weight'>;
This creates a new type called PersonInfo that only includes the name and age properties. The Pick and Omit utility types are related in that they both allow you to create custom types from existing types. However, the Pick utility type is used to select only certain properties from an existing type, while the Omit utility type is used to exclude certain properties from an existing type. The Omit utility type should be used when you need to create a new type from an existing type that excludes certain properties.
Required
The Required utility type is used to make all properties of a type required. For example, if you have an interface with multiple optional properties, you can use Required to make all of the properties required.
For example, suppose you have an interface called Person that has the following properties:
interface Person {
name?: string;
age?: number;
height?: number;
weight?: number;
}
You can use Required to make all of the properties required:
type PersonInfo = Required<Person>;
This creates a new type called PersonInfo that requires all of the properties. The Required utility type should be used when you need to make all of the properties of a type required, or to ensure that all properties of an existing type are used.
Partial
The Partial utility type is used to create a type that contains only some of the properties of an existing type. For example, if you have an interface with multiple properties, you can use Partial to create a new type that includes only a subset of the properties.
For example, suppose you have an interface called Person that has the following properties:
interface Person {
name: string;
age: number;
height: number;
weight: number;
}
You can use Partial to create a new type that only includes the name and age properties:
type PersonInfo = Partial<Person>;
This creates a new type called PersonInfo that only includes the name and age properties, and all of the properties are optional. The Partial utility type should be used when you need to create a new type from an existing type that contains only some of the properties to make existing types more flexible.
Record
The Record utility type is used to create a type of a specific set of keys and corresponding values. For example, if you have a collection of objects with different keys and values, you can use Record to create a type that only includes the keys and values you need.
For example, suppose you have an array of objects with the following keys and values:
const people = [
{ name: 'John', age: 30 },
{ name: 'Mary', age: 25 },
{ name: 'Alice', age: 20 }
];
You can use Record to create a type that only includes the name and age properties:
type PersonInfo = Record<string, { name: string, age: number }>;
This creates a new type called PersonInfo that only has two properties: name and age. The Record utility type should be used when you need to create a type that has a specific set of keys and corresponding values. is not better to use an interface or a type for this purpose?
It depends on the situation. An interface is usually recommended when you need to define a type that will be used in multiple places and need to ensure that the type has consistent properties across the project. However, a type can be used if you only need to define a type for specific purposes and don’t need the type to be consistent across the project. In addition, the utility types can be used to easily construct custom types that can be used in place of interfaces or types.
Exclude
The Exclude utility type is used to create a type that excludes certain specific types. For example, if you have a union type of multiple possible types, you can use Exclude to create a type that excludes one or more of the types.
For example, suppose you have a union type of string and number:
type StrNumUnion = string | number;
You can use Exclude to create a type that excludes the number type:
type StrOnlyUnion = Exclude<StrNumUnion, number>;
This creates a new type called StrOnlyUnion that only includes the string type. The Exclude utility type should be used when you need to create a type that excludes certain specific types. It can be used to create custom types for use in a project, or to simplify existing types for use in other operations. How is this different from the Omit type? The Omit utility type is used to exclude certain properties from an existing type, while the Exclude utility type is used to create a type that excludes certain specific types.
ReadOnly
The Readonly utility type is used to make all properties of a type read-only. For example, if you have an interface with multiple properties, you can use Readonly to make all of the properties read-only.
For example, suppose you have an interface called Person that has the following properties:
interface Person {
name: string;
age: number;
height: number;
weight: number;
}
You can use Readonly to make all of the properties read-only:
type PersonInfo = Readonly<Person>;
This creates a new type called PersonInfo that has all of the properties as read-only. The Readonly utility type should be used when you need to make all of the properties of a type read-only.
Constructor
The Constructor utility type is used to create a type that is a constructor function. For example, if you have a class with multiple properties, you can use Constructor to create a type that is a constructor function for the class.
For example, suppose you have a class called Person with the following properties:
class Person {
name: string;
age: number;
height: number;
weight: number;
}
You can use Constructor to create a type that is a constructor function for the class:
type PersonConstructor = Constructor<Person>;
This creates a new type called PersonConstructor that is a constructor function for the Person class. The Constructor utility type should be used when you need to create a type that is a constructor function for a class.
NonNullable
The NonNullable utility type is used to create a type that is non-nullable. For example, if you have a union type that includes null, you can use NonNullable to create a type that excludes null.
For example, suppose you have a union type that includes null:
type StrNumUnion = string | number | null;
You can use NonNullable to create a type that excludes null:
type StrNumNonNullable = NonNullable<StrNumUnion>;
This creates a new type called StrNumNonNullable that only includes the string and number types. The NonNullable utility type should be used when you need to create a type that excludes null. It can be used to create custom types for use in a project, or to simplify existing types for use in other operations.
Latest comments (0)