TypeDrop

2026-05-18 Challenge

2026-05-18 Easy

Typed Product Inventory Grouper

You're building the catalog layer for an e-commerce platform. Raw product entries arrive as `unknown` from a warehouse API; your engine must validate them, group them by category, and return a strongly-typed per-category inventory summary — with zero `any`.

Goals

  • Define the `Product` interface and `ParseResult<T>` discriminated union with correct field constraints.
  • Implement `parseProduct(raw: unknown)` to validate every field at runtime and return a typed `ParseResult<Product>` — no `any` or type assertions.
  • Implement `buildInventoryReport` to parse all entries, group valid products by category, and compute per-category `CategorySummary` values.
  • Return a fully-typed `InventoryReport` with summaries sorted alphabetically by category and all parse errors collected.
challenge.ts
// Core types and main function signature

interface Product {
  id:        string;
  name:      string;
  category:  string;
  price:     number;   // must be > 0
  stock:     number;   // must be >= 0 and an integer
  available: boolean;
}

type ParseResult<T> =
  | { ok: true;  value: T }
  | { ok: false; error: string };

interface CategorySummary {
  category:      string;
  productCount:  number;
  totalStock:    number;
  totalValue:    number;   // sum of price * stock
  availableCount: number;
}

interface InventoryReport {
  summaries:     CategorySummary[];
  parseErrors:   string[];
  totalProducts: number;
}

export function parseProduct(raw: unknown): ParseResult<Product> { /* TODO */ }
export function buildInventoryReport(rawProducts: unknown[]): InventoryReport { /* TODO */ }
Hints (click to reveal)

Hints

  • Use `typeof raw === 'object' && raw !== null && 'price' in raw` style guards to safely narrow `unknown` — then access properties through the narrowed type.
  • A `Map<string, Product[]>` is a clean way to accumulate products per category before converting to `CategorySummary[]`.
  • Remember: `Number.isInteger(n)` and `n >= 0` are two separate checks needed for the `stock` constraint.

Or clone locally

git clone -b challenge/2026-05-18 https://github.com/niltonheck/typedrop.git