ProgrammingFullstack Developer

What are 'utility types' in TypeScript (for example, Partial, Required, Pick, Omit), where and how should they be applied, and what mistakes are often made in their usage?

Pass interviews with Hintsage AI assistant

Answer

Utility types are built-in TypeScript tools for transforming, modifying, or deriving types based on existing ones. The most commonly encountered:

  • Partial<T> — makes all properties of type T optional
  • Required<T> — makes all properties mandatory
  • Pick<T, K> — selects properties K from type T only
  • Omit<T, K> — excludes properties K from type T

Example usage:

type User = { id: number; name: string; age?: number }; type UserPreview = Pick<User, 'id' | 'name'>; type UserPatch = Partial<User>; type FullUser = Required<User>; type UserWithoutAge = Omit<User, 'age'>;

This allows for creating more flexible APIs and DTOs.

Trick question

If a property of the type is specified as optional (age?: number), how will Required<T> behave? Will it remain optional?

Answer: No, Required<T> transforms all properties of type T, including optional ones, into mandatory (required).

type User = { age?: number }; type UserRequired = Required<User>; // UserRequired: { age: number }

Examples of real mistakes due to lack of understanding of the nuances


Story

Used Partial<T> for updating objects, thinking it was automatically safe for the database. Forgot to check fields for null/undefined and faced validation errors when updating entities.


Story

Used Omit<T, K> to exclude technical properties from the request type, not considering that during model refactoring new technical fields were not added to the exclusion list. As a result, private fields "leaked" through the API.


Story

In the Shared library, created type Pick<T, K>, making a mistake in the list of keys: K was partially missing from the original type. TypeScript missed the error when using literal strings, but when the original type changed, there was a mismatch, and part of the types "broke" across all services.