What utility types exist and their usage
TypeScript provides a set of utility types that can be used to create and manipulate types in a more expressive and flexible way. Some of the utility types provided by TypeScript are:
Partial<T>
: This type creates a new type that represents the partial version of a typeT
. The partial version of a type has all the properties of the original type, but they are all optional. This type is useful for creating types that represent a subset of the properties of another type, or for creating types that can be used to partially update the properties of another type.Readonly<T>
: This type creates a new type that represents the read-only version of a typeT
. The read-only version of a type has all the properties of the original type, but they are all read-only. This type is useful for creating types that cannot be modified, and for creating types that can only be used for reading the properties of another type.Pick<T, K>
: This type creates a new type that represents a subset of the properties of a typeT
, selected by a set of keysK
. This type is useful for creating types that represent a subset of the properties of another type, based on a specific set of keys.Omit<T, K>
: This type creates a new type that represents a subset of the properties of a typeT
, excluding a set of keysK
. This type is useful for creating types that represent a subset of the properties of another type, excluding a specific set of keys.Record<K, T>
: This type creates a new type that represents an object with a set of keysK
and values of typeT
. This type is useful for creating types that represent objects with a specific set of keys and values.Exclude<T, U>
: This type creates a new type that represents the set of values that are in a typeT
but not in a typeU
. This type is useful for creating types that represent the set of values that are in one type but not in another type.Extract<T, U>
: This type creates a new type that represents the set of values that are in a typeT
and also in a typeU
. This type is useful for creating types that represent the set of values that are in both types.ReturnType<T>
: This type creates a new type that represents the return type of a function or a methodT
. This type is useful for creating types that represent the return type of a function or a method.InstanceType<T>
: This type creates a new type that represents the instance type of a class or a constructor functionT
. This type is useful for creating types that represent the instance type of a class or a constructor function.
These are some of the utility types provided by TypeScript, and they can be used to create and manipulate types in a more expressive and flexible way. It is important to understand the role and the use of these utility types in TypeScript, and to use them appropriately in your code.