Lesson 7 of 8
Practice & Quiz - Full-Stack TypeScript Architecture
Test your mastery of production TypeScript - monorepos, shared types, CI pipelines, performance best practices, and the complete layered architecture - with a capstone exercise and 5 quiz questions.
Practical: Shared Types Monorepo Package
// packages/shared/src/index.ts - the single source of truth for API contracts
import { z } from "zod";
// ── Request / Response schemas (used by both server and client) ──
export const CreateTaskSchema = z.object({
title: z.string().min(1).max(200),
priority: z.enum(["low", "normal", "high"]).default("normal"),
dueDate: z.string().datetime().optional(),
});
export const TaskSchema = z.object({
id: z.number(),
title: z.string(),
status: z.enum(["PENDING", "IN_PROGRESS", "COMPLETED"]),
priority: z.enum(["low", "normal", "high"]),
dueDate: z.string().nullable(),
createdAt: z.string(),
updatedAt: z.string(),
});
// ── TypeScript types derived from schemas - always in sync ───
export type CreateTaskDTO = z.infer<typeof CreateTaskSchema>;
export type Task = z.infer<typeof TaskSchema>;
// ── Typed API response envelope ──────────────────────────────
export type ApiSuccess<T> = { ok: true; data: T; timestamp: string };
export type ApiError<E> = { ok: false; error: E; timestamp: string };
export type ApiResponse<T, E = string> = ApiSuccess<T> | ApiFailure<E>;
// packages/server/src/controllers/TaskController.ts - uses shared types
import { CreateTaskSchema, type CreateTaskDTO } from "@acme/shared";
async function createTask(req: Request, res: Response): Promise<void> {
const parsed = CreateTaskSchema.safeParse(req.body);
if (!parsed.success) {
res.status(422).json({ ok: false, error: parsed.error.flatten() });
return;
}
const dto: CreateTaskDTO = parsed.data; // guaranteed typed ✅
// ... call service, return ApiResponse<Task>
}
// packages/web/src/api/taskApi.ts - same shared types on the client
import { TaskSchema, type Task } from "@acme/shared";
async function getTask(id: number): Promise<Task> {
const raw = await fetch(`/api/tasks/${id}`).then(r => r.json());
return TaskSchema.parse(raw); // runtime validation + type narrowing ✅
}Module Quiz
What is the primary benefit of a `packages/shared` types package in a TypeScript monorepo?
Tip
Tip
Practice Practice Quiz FullStack TypeScript Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
TypeScript = JavaScript + static types → fewer bugs, better tooling
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz FullStack TypeScript Architecture 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.
Common Mistake
Warning
A common mistake with Practice Quiz FullStack TypeScript Architecture is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Finished this lesson?