TypeDrop
2026-02-20 Challenge
2026-02-20
Easy
Typed Inventory Aggregator
You're building a dashboard for a small e-commerce warehouse. Given a flat list of product entries (each with a category, SKU, price, and stock count), aggregate them into a per-category summary — the hardest part is getting the TypeScript types exactly right.
Goals
- Define a branded `Sku` type and implement `toSku` without using `any` or a type assertion.
- Build the `InventoryItem` and `CategorySummary` interfaces with all required fields typed correctly.
- Implement `aggregateInventory` to produce a correct per-category summary in a single pass over the items array.
- Implement `topCategories` returning category names sorted by total inventory value descending, capped at n results.
challenge.ts
// Key types & main function signature
type Sku = string & { readonly _brand: "Sku" };
interface InventoryItem {
sku: Sku;
name: string;
category: string;
priceUsd: number;
stock: number;
}
interface CategorySummary {
totalItems: number;
totalStock: number;
totalValueUsd: number;
cheapestSku: Sku;
mostStockedSku: Sku;
}
type InventorySummary = Record<string, CategorySummary>;
function aggregateInventory(items: InventoryItem[]): InventorySummary { ... }
function topCategories(summary: InventorySummary, n: number): string[] { ... }
Hints (click to reveal)
Hints
- For `toSku`, look at how TypeScript's `satisfies` operator or a simple return-type annotation can coerce a `string` to a branded type without writing `as Sku`.
- Use a `for...of` loop or `Array.reduce` over the items and build up the `InventorySummary` object incrementally — initialise each category bucket on first encounter.
- For `topCategories`, `Object.entries` gives you `[category, summary]` pairs you can sort by `totalValueUsd` before slicing to `n`.
Useful resources
Or clone locally
git clone -b challenge/2026-02-20 https://github.com/niltonheck/typedrop.git