Lessons in this course (9)

6. Utility types

Intermediate★ Must knowLesson 6 of 9
UtilityResultTypical use
Partial<T>All properties optionalPATCH/update models
Required<T>All requiredNormalised config
Readonly<T>All readonlyImmutable state
Pick<T, K>Only keys KDTO with a few fields
Omit<T, K>Without keys KCreate model without id
Record<K, V>Object with keys K of type VLookup maps
Exclude<U, X> / Extract<U, X>Filter a unionNarrow status lists
NonNullable<T>Remove null/undefinedAfter validation
ReturnType<F> / Parameters<F>Function typesReuse inferred types
Awaited<T>Unwrap PromiseResult 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>>;