Lesson 8 of 8
Mini Project: Task Manager - Part 2 (Type-Rich Domain)
Extend the Task Manager domain with discriminated unions for task state transitions, index signatures for a task registry, `satisfies` for typed config, and `readonly` arrays throughout. The domain model becomes the single source of truth for the entire app.
Task Manager Part 2 - Advanced Domain Types
// src/types/domain.ts - extended
// ── Task lifecycle - discriminated union ─────────────────────
export type TaskState =
| { phase: "pending"; createdAt: Date }
| { phase: "active"; startedAt: Date; assigneeId: number }
| { phase: "blocked"; reason: string; blockedAt: Date }
| { phase: "completed"; completedAt: Date; verifiedBy: number | null }
| { phase: "cancelled"; cancelledAt: Date; cancelReason: string };
export interface Task {
readonly id: number;
title: string;
description: string | null;
state: TaskState;
priority: Priority;
tags: readonly string[];
readonly createdAt: Date;
updatedAt: Date;
}
// ── Task event log - discriminated actions ────────────────────
export type TaskEvent =
| { event: "created"; taskId: number; by: number; at: Date }
| { event: "started"; taskId: number; by: number; at: Date }
| { event: "blocked"; taskId: number; reason: string; at: Date }
| { event: "completed"; taskId: number; at: Date }
| { event: "cancelled"; taskId: number; reason: string; at: Date };
// ── Task registry - typed index access ───────────────────────
export type TaskRegistry = Record<number, Task>;
// ── App config - satisfies for validation + narrow types ──────
const APP_CONFIG = {
maxTasksPerUser: 100,
defaultPriority: "medium",
apiBaseUrl: "https://api.task-manager.example.com",
featureFlags: {
subtasks: true,
attachments: false,
aiSuggestions: false,
},
} satisfies {
maxTasksPerUser: number;
defaultPriority: Priority;
apiBaseUrl: string;
featureFlags: Record<string, boolean>;
};
// Types preserved: APP_CONFIG.defaultPriority is "medium", not Priority
export type AppConfig = typeof APP_CONFIG;
// ── src/services/taskRegistry.ts ──────────────────────────────
import type { Task, TaskRegistry, TaskEvent } from "../types/domain";
export class TaskRegistry {
private tasks: TaskRegistry = {};
private events: TaskEvent[] = [];
add(task: Task): void {
this.tasks[task.id] = task;
this.events.push({ event: "created", taskId: task.id, by: 0, at: new Date() });
}
get(id: number): Task | undefined {
return this.tasks[id];
}
getByPhase(phase: TaskState["phase"]): Task[] {
return Object.values(this.tasks).filter(t => t.state.phase === phase);
}
getLog(): readonly TaskEvent[] {
return this.events; // readonly - callers cannot push to internal log
}
}Common Mistakes
- Making `TaskRegistry` a mutable `Map<number, Task>` without type constraints - using `Record<number, Task>` with a method interface is cleaner and more type-safe.
- Returning mutable internal arrays to callers - always return `readonly T[]` from methods that expose internal state. This prevents external code from modifying state it doesn't own.
- Using string IDs in the registry without validation - `Record<number, Task>` rejects string keys at compile time. Stick to the same ID type throughout the domain.
Tip
Tip
Practice Mini Project Task Manager Part 2 TypeRich Domain in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Mini Project: Task Manager - Part 2 (Type-Rich Domain)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Task Manager Part 2 TypeRich Domain from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Task Manager Part 2 TypeRich Domain is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- Extend the Task Manager domain with discriminated unions for task state transitions, index signatures for a task registry, `satisfies` for typed config, and `readonly` arrays throughout.
- Making `TaskRegistry` a mutable `Map<number, Task>` without type constraints - using `Record<number, Task>` with a method interface is cleaner and more type-safe.
- Returning mutable internal arrays to callers - always return `readonly T[]` from methods that expose internal state. This prevents external code from modifying state it doesn't own.
- Using string IDs in the registry without validation - `Record<number, Task>` rejects string keys at compile time. Stick to the same ID type throughout the domain.
Finished this lesson?