Utility Types | Typescript
Partial<T>#
Constructs a type with all properties of T set to optional. This utility will return a type that represents all subsets of a given type.
Example#
Readonly<T>#
Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.
Example#
This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a frozen object).
Object.freeze#
Record<K,T>#
Constructs a type with a set of properties K of type T. This utility can be used to map the properties of a type to another type.
Example#
Pick<T,K>#
Constructs a type by picking the set of properties K from T.
Example#
Omit<T,K>#
Constructs a type by picking all properties from T and then removing K.
Example#
Exclude<T,U>#
Constructs a type by excluding from T all properties that are assignable to U.
Example#
Extract<T,U>#
Constructs a type by extracting from T all properties that are assignable to U.
Example#
NonNullable<T>#
Constructs a type by excluding null and undefined from T.
Example#
Parameters<T>#
Constructs a tuple type of the types of the parameters of a function type T.
Example#
ConstructorParameters<T>#
The ConstructorParameters<T> type lets us extract all parameter types of a constructor function type. It produces a tuple type with all the parameter types (or the type never if T is not a function).
Example#
ReturnType<T>#
Constructs a type consisting of the return type of function T.
Example#
InstanceType<T>#
Constructs a type consisting of the instance type of a constructor function type T.
Example#
Required<T>#
Constructs a type consisting of all properties of T set to required.
Example#
ThisParameterType#
Extracts the type of the this parameter of a function type, or unknown if the function type has no this parameter.
Note: This type only works correctly if --strictFunctionTypes is enabled. See #32964.
Example#
OmitThisParameter#
Removes the 'this' parameter from a function type.
Note: This type only works correctly if --strictFunctionTypes is enabled. See #32964.
Example#
ThisType<T>#
This utility does not return a transformed type. Instead, it serves as a marker for a contextual this type. Note that the --noImplicitThis flag must be enabled to use this utility.
Example#
In the example above, the methods object in the argument to makeObject has a contextual type that includes ThisType<D & M> and therefore the type of this in methods within the methods object is { x: number, y: number } & { moveBy(dx: number, dy: number): number }. Notice how the type of the methods property simultaneously is an inference target and a source for the this type in methods.
The ThisType<T> marker interface is simply an empty interface declared in lib.d.ts. Beyond being recognized in the contextual type of an object literal, the interface acts like any empty interface.