TypeScript for Real Projects · 24 min · 160 XP
Derive types and safe updates
Derive update contracts from a domain model and understand what utility types do at runtime.
Repeated object shapes drift when one copy changes. Pick selects fields, Omit removes fields from a type, Partial makes fields optional, and Readonly prevents assignment through that reference. Indexed access such as Profile['id'] reuses a field's type. These operations describe contracts; none copies, deletes or freezes an object at runtime.
type Profile = { readonly id: string; name: string; bio: string; admin: boolean };
type ProfilePatch = Partial<Pick<Profile, "name" | "bio">>;
type ProfileId = Profile["id"];
const draft = { name: "Mina" } satisfies ProfilePatch;satisfies checks compatibility while retaining the expression's useful inferred type; it does not validate runtime data. Partial is shallow: it does not recursively make nested fields optional. With exactOptionalPropertyTypes, an optional string property may be omitted but cannot explicitly be undefined unless the type includes undefined. A type also does not guarantee that an object has no additional fields: variables can carry more than a target shape requires. Copy permitted fields explicitly at an update boundary.
Loading your workspace…