TypeDrop
A new TypeScript challenge every day. Sharpen your types.
TypeDrop delivers a fresh TypeScript challenge every day, generated by AI. Pick a challenge, open it in StackBlitz (preferred) or CodeSandbox (or clone it locally), and make the tests pass. No accounts, no setup — just you and the type system.
2026-12-03
Medium
Typed Inventory Tracker: Stock Mutations, Low-Stock Alerts & Audit Log
You're building the inventory management core for a small e-commerce warehouse. Products arrive in shipments, get reserved for orders, and are adjusted for damage — every mutation must be tracked in a typed audit log, and the system must surface low-stock alerts with enough context for the ops team to act on them.
Goals
- Implement `applyMutation` to validate and apply each discriminated-union mutation variant, updating state in place and returning a typed MutationResult.
- Implement `getAvailableStock` to derive available quantity per product and return results sorted ascending.
- Implement `getLowStockAlerts` to detect products at or below their threshold, assigning severity and sorting critical alerts before warnings.
- Implement `getAuditTrail` to filter the append-only log by skuId and optionally by MutationKind, preserving chronological order.
challenge.ts
// Core domain types + main function signatures
export type MutationKind =
| "restock" | "reservation" | "release" | "sale" | "adjustment";
export type StockMutation =
| { kind: "restock"; skuId: string; qty: number; supplierId: string }
| { kind: "reservation"; skuId: string; qty: number; orderId: string }
| { kind: "release"; skuId: string; qty: number; orderId: string }
| { kind: "sale"; skuId: string; qty: number; orderId: string }
| { kind: "adjustment"; skuId: string; qty: number; reason: string; delta: number };
export type MutationResult =
| { ok: true; entry: AuditEntry }
| { ok: false; error: string };
// Apply one mutation → validates rules, updates state, appends audit entry
export function applyMutation(
state: InventoryState,
mutation: StockMutation
): MutationResult { ... }
// available = onHand − reserved, sorted ascending
export function getAvailableStock(state: InventoryState): AvailableStock[] { ... }
// Products at/below alertThreshold → critical (0) first, then warning
export function getLowStockAlerts(state: InventoryState): LowStockAlert[] { ... }
// All audit entries for a SKU, optionally filtered by MutationKind
export function getAuditTrail(
state: InventoryState, skuId: string, filter?: MutationKind
): AuditEntry[] { ... }
Hints (click to reveal)
Hints
- Use a `switch` on `mutation.kind` to exhaustively narrow the discriminated union — the compiler will warn you if you miss a branch.
- The `MutationResult` union lets you return either `{ ok: true, entry }` or `{ ok: false, error }` without any type assertions — lean on it for all validation failures.
- For `getLowStockAlerts`, compute `available` once per product and compare it to `alertThreshold` before deciding severity — a small helper function keeps the logic clean.
Useful resources
Or clone locally
git clone -b challenge/2026-12-03 https://github.com/niltonheck/typedrop.git