Utility types
This is a reference guide for globally-available utility types for common type transformations.
You don't need to import these types to use them in your TypeScript programs.
Awaited<Type>
Produces the type returned by a Promise
object.
Awaited
will recursively unwrap the result type of a Promise
, in case the Promise
result is another Promise
.
Awaited
is especially useful when working with functions that return promises, where you want to extract the value that the promise resolves to.
Partial<Type>
Returns a new version of Type
where all properties are optional.
Required<Type>
Returns a new version of Type
where all properties are required.
Can be used with Partial
and &
to join a partial and required segment of an object.
Readonly<Type>
Returns a new version of Type
where all properties are set to readonly
. This will produce a type
error
- why freeze an object
Record<Key, Value>
Produces an object type where keys are of type Key
and values are of Value
.
This utility is often used to define a mapping from values of a particular type to values of another type. In many cases, it is an alternate way to write an object index signature.
When writing an index signature, the key type must be string
. Record
allows us to use more specific types as keys, like a particular set of known strings.
When defining a specific key type, each key must be present - TypeScript will catch missing or extra keys.
Some more examples.
You can construct your own Record
type with object index signatures.
Pick<Type, Keys>
Creates a new type by selecting one or more properties from an object type.
It is useful when you only need a subset of fields from a larger type.
You can implement your own Pick
with an index signature type.
The MyPick
type is an improved utility type from the TypeScript FAQs.
Omit<Type, Keys>
Creates a new type by removing one or more properties from an object type.
It is the inverse of Pick
, and is useful when you want most of an object without a few fields.
It is often useful when specifying object operations.
You can create your own Omit
by combining Pick
and Exclude
.
The MyOmit
type is an improved utility type from the TypeScript FAQs that adds key constraints.
Exclude<Type, Members>
Exclude all members of Type
which are assignable to Members
.
Exclude
is useful for filtering out unwanted members from a union type.
Exclude
can be used to partition a union type into more specific subsets.
Exclude
works by producing never
for any type which extends the exclusion type.
Extract<Type, Members>
Only include members of Type
which are assignable to Members
.
Extract
does the opposite of Exclude
by only keeping the types that are assignable to another type. It is often useful for filtering in what you want from a union type.
You can also match object types based on structure, allowing you to filter a union of object types by key-value patterns.
Extract
is implemented by returning never
for types that do not extend the member type.
NonNullable<Type>
Excludes null
and undefined
from Type
.
It is useful when you are certain that a value will be present, and you want TypeScript to enforce that.
You can implement NonNullable
by returning the never
type for null
and undefined
.
Be careful not to overuse NonNullable
— it is best applied after you've actually checked for null
/undefined
, or in places where you know the value is safe.
Parameters<Type>
Constructs a tuple type from the parameters of the given function.
It is useful when you want to reuse or infer the argument types from a function elsewhere.
Suppose you have a function that you want to wrap with another function. You can ensure the arguments of the wrapping function always match the arguments of the function it is calling, to safely pass the arguments of one function to another.
You can construct the equivalent of Parameters
with conditional types and infer
.
ConstructorParameters<Type>
Constructs a tuple or array type from the constructor parameters.
Produces the never
type if Type
is not a class.
You can also use ConstructorParameters
with an interface that describes a class definition.
ReturnType<Type>
Extracts the return type of a function type.
It is a useful way to keep types in sync, especially when working with callbacks and APIs.
It is also useful in situations where you might otherwise have to type out the structure of a function's return type.
You can implement ReturnType
with infer
.
InstanceType<Type>
Extracts the type of an instance created by a constructor function or class.
It is useful for inferring the result of new MyClass()
, especially when you are working with arbitrary classes.
This is useful in real situations where you want to dynamically create a class.
Also works with constructor functions.
ThisParameterType<Type>
Extracts the type of this
, or unknown
if function type has no this
parameter.
OmitThisParameterType<Type>
Omits the this
parameter type.
ThisType<Type>
Does not return a transformed type, but serves as a marker for contextual this
.