TypeScript Intermediate: Utility Types, Narrowing & Advanced Types

Lesson 1 of 7

Utility Types: Partial, Pick, Omit & Record

TypeScript ships a set of utility types that build new types out of ones you already have, instead of writing everything from scratch. Four of the most common: Partial, Pick, Omit, and Record.

TypeScript

The four utility types

UtilityWhat it does
Partial<T>Makes every property of T optional
Pick<T, K>Keeps only the listed keys K
Omit<T, K>Keeps everything except the listed keys K
Record<K, V>Builds an object type with keys K, all mapped to value type V

Partial<Omit<Task, 'id'>> above composes two of them: drop id entirely, then make what's left optional, exactly what an "update" function needs, callers shouldn't be able to change a task's id, and shouldn't have to repeat every other field just to change one.

Record for lookup tables

TypeScript

Task['priority'] reads the type of the priority property directly off Task, so board is guaranteed to have exactly the three keys the union allows, no more, no less. Try board.urgent and the compiler rejects it before the code runs.

NOTE

None of these are special compiler magic, Partial<T>, Pick<T, K>, and friends are themselves just TypeScript types defined in the standard library using mapped types, the tool you'll build your own versions of in the next lesson.

📝 Utility Types Quiz

Passing score: 70%
  1. 1.What does `Omit<Task, 'id'>` produce?

  2. 2.The utility type that makes every property of an existing type optional is ____.

  3. 3.Record<K, V> requires K to be a string, number, or symbol (or a union of them).

  4. 4.Which utility type keeps only the listed keys of an existing type?

  5. 5.Task['priority'] reads the type of Task's priority property directly off the interface.