Advanced TypeScript

Utility Types

TypeScript utility types are standard reusable transformations such as Partial, Required, Pick, Omit, Record, ReturnType, Parameters, Extract, Exclude, NonNullable, and Awaited.
  • Utility types encode common transformations
  • Pick/Omit are shape tools, not DTO design strategy
  • Partial is easy to overuse
  • Record is useful for dictionaries with known key sets
  • ReturnType and Parameters couple to implementation
Common utility types
UtilityMeaningExample use
Partial<T>All properties optionalDraft/update object
Required<T>All properties requiredNormalized config
Pick<T, K>Keep selected propertiesPreview DTO
Omit<T, K>Remove selected propertiesPublic shape without secret
Record<K, T>Map keys to valuesHandler table
ReturnType<F>Function return typeWrapper/helper
Extract<T, U>Keep union members assignable to UFilter variants
Exclude<T, U>Remove union members assignable to UFilter variants
Handler table with Record
type Status = "idle" | "loading" | "success" | "error"

type Handler = () => void

const handlers: Record<Status, Handler> = {
  idle: () => {},
  loading: () => {},
  success: () => {},
  error: () => {},
}
Adding a new status forces the table to be updated.
Sources
  • The TypeScript HandbookUtility Types
  • Effective TypeScript, 2nd EditionType design
  • TypeScript ReferenceUtility types