TypeDrop
2026-03-15 Challenge
2026-03-15
Hard
Typed Real-Time Event Stream Processor
You're building the analytics backbone for a live dashboard that ingests a heterogeneous stream of server-sent events (user actions, system alerts, and metric snapshots). Each event must be parsed from raw `unknown` input, routed through a typed middleware pipeline, and aggregated into a strongly-typed per-event-kind summary — all with zero `any`.
Goals
- Implement `parseEvent` to safely narrow `unknown` input into a `StreamEvent` discriminated union, returning typed `Result<StreamEvent, ParseError>` with specific error codes.
- Define `StreamSummary` as a mapped type over `EventKind` using the `KindSummaryMap` helper, then implement `buildSummary` for a single-pass aggregation.
- Implement `runPipeline` using `Extract<StreamEvent, { kind: K }>` to route each event to its correctly-typed middleware without any type assertions.
- Wire everything together in `processStream`, accumulating parse errors and discarding middleware-dropped events before producing the final summary.
challenge.ts
// Core discriminated union
type StreamEvent = UserActionEvent | SystemAlertEvent | MetricSnapshotEvent;
// Per-kind middleware — E is bound to the correct event subtype
type Middleware<E extends StreamEvent = StreamEvent> = (
event: E,
next: (event: E) => E | null
) => E | null;
// Mapped middleware map — keys tied to EventKind, values typed via Extract
type KindMiddlewareMap = {
[K in EventKind]?: Middleware<Extract<StreamEvent, { kind: K }>>;
};
// StreamSummary: a mapped type you must define (Requirement 6)
type StreamSummary = unknown; // TODO: replace with mapped type over EventKind
// Main orchestrator
function processStream(
rawPayloads: unknown[],
middlewareMap: KindMiddlewareMap
): { summary: StreamSummary; parseErrors: ParseError[] } {
throw new Error("Not implemented");
}
Hints (click to reveal)
Hints
- For `StreamSummary`, think `{ [K in EventKind]: KindSummaryMap[K] }` — the mapped type directly indexes your helper map.
- In `runPipeline`, the middleware map value has type `Middleware<Extract<StreamEvent, { kind: typeof event.kind }>>` — narrow the event's kind first, then look it up.
- For `parseEvent`, write a helper like `isObject(v: unknown): v is Record<string, unknown>` to safely access fields before checking their types.
Useful resources
Or clone locally
git clone -b challenge/2026-03-15 https://github.com/niltonheck/typedrop.git