Skip to the solution
MediumFrontend10 min read

What are utility types like Partial, Pick, and Omit?

595 views

The problem

Question presented to candidate: "TypeScript ships utility types like Partial, Pick and Omit. What do they do, how are they built, and when would you reach for each one in real code?"

What a strong answer should cover:

  • They are generic types in the standard library that derive a new object type from an existing one, so one source type stays the single source of truth
  • Partial<T> makes every property optional, Required<T> makes every property mandatory, Readonly<T> makes every property readonly, Pick<T, K> keeps keys K, Omit<T, K> drops keys K
  • They are mapped types over keyof T (Omit is Pick plus Exclude), and they are shallow: nested objects are not changed
  • Typical uses: update patches (Partial<Omit<User, "id">>), create inputs (Omit<User, "id">), previews (Pick), and combining them (make only some keys optional)
  • Traps: Omit does not check its keys, Omit on a union collapses it to the shared keys, and Partial lets a spread overwrite a value with undefined

Clarifying questions expected:

  • Should the derived type change if the source type changes (derive it), or is it a separate contract (write it out)?
  • Is exactOptionalPropertyTypes on? It changes what an optional property may hold

Code / implementation expected: Optional β€” derive a patch and a create type from one interface, or write Partial by hand as a mapped type.

  • utility-types

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

10 min read Β· 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptDeriving patch, preview and create types from one User type
TypeScriptShallow Readonly, PartialBy, Omit on unions and explicit undefined

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 40 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What does the 'strict' flag in tsconfig enable?

Continue