Use TypeScript as an architecture tool: validate at runtime boundaries, model states with discriminated unions, brand IDs, share contracts between client and server, and keep a large codebase's types fast.
TypeScript checks your code, not your data. Every place data enters your program is a boundary where types can silently lie:
const user = (await res.json()) as UserlocalStorage, URL params, postMessage, WebSocket messagesas User is a promise you make to the compiler, not a check. When the backend renames fullName to name, every type still passes and production shows undefined.
The architectural principle: parse, don't validate. At each boundary, run the data through a parser that either returns a correctly typed value or fails loudly. Inside the boundary, trust the types completely.
1import { z } from "zod";
2
3const User = z.object({
4 id: z.string(),
5 name: z.string(),
6 role: z.enum(["admin", "member"]),
7});
8type User = z.infer<typeof User>; // one definition: runtime schema + static type
9
10const user = User.parse(await res.json()); // throws with a precise error if the shape is wrongZod, Valibot, and ArkType all derive the static type from the runtime schema, so the two can't drift apart.