TypeScript Utility Types

I want to read more about TypeScript Utility types. (https://www.typescriptlang.org/docs/handbook/utility-types.html)

Date Created:
1 768

References



Notes


TypeScript offers a variety of utility types to facilitate common type transformations. Here's a summary of some of the key utility types:

  • Partial<Type>: Constructs a type with all properties of Type set to optional, representing all subsets of a given type.
  • Required<Type>: Constructs a type with all properties of Type set to required, the opposite of Partial.
  • Readonly<Type>: Constructs a type with all properties of Type set to readonly, preventing modification of the properties.
  • Record<Keys, Type>: Constructs a type with a set of properties Keys of type Type.
  • Pick<Type, Keys>: Constructs a type by picking a set of properties Keys from Type.
  • Omit<Type, Keys>: Constructs a type by omitting a set of properties Keys from Type.
  • Exclude<UnionType, ExcludedMembers>: Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers.
  • Extract<UnionType, ExtractedMembers>: Constructs a type by extracting from UnionType all union members that are assignable to ExtractedMembers.
  • NonNullable<Type>: Constructs a type by excluding null and undefined from Type.
  • Parameters<Type>: Constructs a tuple type from the types used in the parameters of a function type Type.
  • ConstructorParameters<Type>: Constructs a tuple or array type from the types used in the parameters of a constructor function type Type.
  • ReturnType<Type>: Constructs a type consisting of the return type of function Type.
  • InstanceType<Type>: Constructs a type consisting of the instance type of a constructor function type Type.
  • ThisParameterType<Type>: Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.
  • OmitThisParameter<Type>: Removes the this parameter from Type.
  • ThisType<Type>: Used to specify the type of this in methods of an object.
  • Awaited<Type>: Used to model operations like await in async functions, recursively unwrapping Promises.

You can read more about how comments are sorted in this blog post.

User Comments

Frank-McKee-Brown

This is the only content on this site so far that was generated by an AI. It was late one night and I wanted to try to keep the daily reading streak alive, so I had this generated (and changed some formatting). I need to learn how to manage TypeScript types better.

0 1 15