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.
The four utility types
| Utility | What 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
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.