Utility Types: Partial, Pick, Omit, Record
TypeScript has built-in helpers that make new types from existing ones, so you don't repeat yourself.
Utility types build new types from existing ones, so you don't repeat yourself: a form where every field is optional (Partial), a card showing only some fields (Pick), a new item without its server-made id (Omit).
Example
Record: an object used as a dictionary
Example
More helpers
Required<T>- every property must be thereReadonly<T>- nothing can be changedReturnType<typeof fn>- the type a function returnsNonNullable<T>- removesnullandundefined
Example
Key points
- Partial, Required, Readonly change all properties.
- Pick and Omit keep or drop properties.
- Record<K, V> describes dictionaries.
📝 Test yourself
1. Which makes every property optional?
2. Omit<Product, "id"> gives you...
3. Fill in the helper for a dictionary of string keys to numbers:
const stock: <string, number> = {};
