Roadmap › TypeScript — Types, Generics and Real-World Patterns › Module: Advanced Types
Lessons in this course (9)
6. Utility types
Intermediate★ Must knowLesson 6 of 9
| Utility | Result | Typical use |
|---|---|---|
Partial<T> | All properties optional | PATCH/update models |
Required<T> | All required | Normalised config |
Readonly<T> | All readonly | Immutable state |
Pick<T, K> | Only keys K | DTO with a few fields |
Omit<T, K> | Without keys K | Create model without id |
Record<K, V> | Object with keys K of type V | Lookup maps |
Exclude<U, X> / Extract<U, X> | Filter a union | Narrow status lists |
NonNullable<T> | Remove null/undefined | After validation |
ReturnType<F> / Parameters<F> | Function types | Reuse inferred types |
Awaited<T> | Unwrap Promise | Result of async function |
typescript
type CreateOrder = Omit<Order, "id" | "status">;
type OrderPatch = Partial<Pick<Order, "status" | "lines">>;
const labels: Record<OrderStatus, string> = { draft: "Draft", submitted: "Submitted", shipped: "Shipped" };
type Loaded = Awaited<ReturnType<typeof loadOrders>>;