Lesson 8 +10 XP

Project: Task Manager & Validation Engine

Project 1: Task Manager & Validation Engine

Build a type-safe Task Management System with automated data validation using interfaces, discriminated unions, and type guards.

Project Architecture

export type Priority = "low" | "medium" | "high";
export type TaskStatus = "todo" | "in_progress" | "completed";

export interface Task {
  id: string;
  title: string;
  description?: string;
  priority: Priority;
  status: TaskStatus;
  createdAt: Date;
}

export type TaskAction =
  | { type: "CREATE"; payload: Omit<Task, "id" | "createdAt"> }
  | { type: "UPDATE_STATUS"; payload: { id: string; status: TaskStatus } }
  | { type: "DELETE"; payload: { id: string } };

Task Manager Implementation

export class TaskManager {
  private tasks: Map<string, Task> = new Map();

  dispatch(action: TaskAction): Task | boolean {
    switch (action.type) {
      case "CREATE": {
        const newTask: Task = {
          id: `task_${Date.now()}`,
          ...action.payload,
          createdAt: new Date()
        };
        this.tasks.set(newTask.id, newTask);
        return newTask;
      }
      case "UPDATE_STATUS": {
        const task = this.tasks.get(action.payload.id);
        if (!task) return false;
        task.status = action.payload.status;
        return task;
      }
      case "DELETE": {
        return this.tasks.delete(action.payload.id);
      }
    }
  }

  getTasksByStatus(status: TaskStatus): Task[] {
    return Array.from(this.tasks.values()).filter((t) => t.status === status);
  }
}

TL;DR

  • Combines Discriminated Unions with Reducer-like actions.
  • Employs Omit utility type for creation payloads.
  • Demonstrates typed Map data storage for domain entities.