TypeDrop
2026-10-06 Challenge
2026-10-06
Easy
Typed Task Priority Queue
You're building the task scheduling layer for a lightweight project management tool. Incoming tasks arrive with different priority levels and metadata; your job is to implement a strongly-typed priority queue that enqueues tasks, dequeues the highest-priority one, and produces a typed summary report — all enforced by the compiler.
Goals
- Define `Priority`, `Task`, `PriorityQueue<T>`, and `SummaryResult` with correct TypeScript types — including the `satisfies` constraint on `PRIORITY_ORDER`.
- Implement `enqueue` and `dequeue` so tasks are retrieved in highest-priority, earliest-creation order.
- Implement `summarize` to return the correct discriminated-union variant with per-priority counts and a non-destructive peek at the top task.
- Ensure everything compiles under `strict: true` with no `any`, no `as`, and no unsafe casts.
challenge.ts
// Key types & main signatures at a glance:
export type Priority = "low" | "medium" | "high";
export interface Task {
id: string;
title: string;
priority: Priority;
tags: readonly string[];
createdAt: Date;
}
export interface PriorityQueue<T> {
items: T[];
size: number;
}
export type SummaryResult =
| { status: "ok"; counts: Record<Priority, number>; topTask: Task }
| { status: "empty"; counts: Record<Priority, number> };
export function enqueue<T extends Task>(queue: PriorityQueue<T>, task: T): PriorityQueue<T>;
export function dequeue<T extends Task>(queue: PriorityQueue<T>): T | undefined;
export function summarize<T extends Task>(queue: PriorityQueue<T>): SummaryResult;
Hints (click to reveal)
Hints
- Use `satisfies Record<Priority, number>` (not a type annotation) on `PRIORITY_ORDER` so TypeScript verifies all three keys exist while still inferring the literal value types.
- The `SummaryResult` discriminated union shares `counts` across both variants — define each variant inline inside the union rather than via a base interface to keep narrowing simple.
- In `dequeue` and `summarize`, sort or reduce over `queue.items` using `PRIORITY_ORDER[task.priority]` as the comparison key; fall back to `createdAt.getTime()` for tie-breaking.
Useful resources
Or clone locally
git clone -b challenge/2026-10-06 https://github.com/niltonheck/typedrop.git