CodePark/ questions / Utility types
START PRACTICINGPRACTICE
Frontend question · TypeScript › Utility types

Name the built-in utility types you use most and what each one does.

The most used utility types fall into three groups: reshaping objects, filtering unions, and reading function types.

For reshaping objects:

  • Partial<T> makes every property optional. It is handy for update payloads, where you send only the changed fields.
  • Required<T> makes every property required.
  • Readonly<T> marks every property readonly.
  • Pick<T, K> keeps only some keys.
  • Omit<T, K> removes some keys.
  • Record<K, V> builds an object type with keys K and values V.

For unions:

  • Exclude<U, X> removes the members that can be assigned to X.
  • Extract<U, X> keeps only those members.
  • NonNullable<T> removes null and undefined.

For functions and promises:

  • ReturnType<F> reads a function's return type.
  • Parameters<F> reads a function's parameter types as a tuple.
  • Awaited<T> unwraps a promise to its resolved value.

These let you build types from existing code instead of writing them twice.

  • Partial, Required and Readonly change the modifiers of every property.
  • Pick and Omit choose which keys to keep, and Record builds key-value object types.
  • Exclude, Extract and NonNullable work on union types.
  • ReturnType, Parameters and Awaited build types from functions and promises.