What are Utility Types?
TypeScript provides several global utility types to help with common type transformations. These allow you to take an existing type and create a new, modified version of it without rewriting the entire definition.
1. Partial<T> and Required<T>
Partial<T>: Makes all properties of a type optional.Required<T>: Makes all properties mandatory (opposite of Partial).
typescript
2. Pick<T, K> and Omit<T, K>
Pick<T, K>: Creates a type by selecting a set of propertiesKfromT.Omit<T, K>: Creates a type by removing a set of propertiesKfromT.
typescript
3. Record<K, T>
Creates an object type whose property keys are
K and whose property values are T.typescript
4. Readonly<T>
Makes all properties of a type immutable.
typescript
Summary
| Type | Transformation | Usage |
|---|---|---|
| Partial | All properties ? | PATCH requests, configs |
| Required | Remove all ? | Validation logic |
| Pick | Choose specific keys | UI components, narrow models |
| Omit | Remove specific keys | Security (hide passwords) |
| Record | Map of Keys to Values | Caches, lookup tables |
| Readonly | All properties readonly | Constants, shared state |
| Exclude | Remove from Union | `Exclude<"a" |
| Extract | Keep from Union | `Extract<"a" |
| NonNullable | Remove null / undefined | Sanitization |
| ReturnType | Get function output type | API response handling |
| Parameters | Get function input types | Higher-order functions |
| Logic | "Don't repeat yourself" - transform existing types! | |
| Standard | Built into TypeScript (No imports needed) |