Object Programs
Object Programs — purpose and prerequisites
Object programs combine related values under named keys and make transformations explicit. Shallow spread copies the top-level properties, not deeply nested objects. Choose readonly property types when callers should treat a record as immutable.
What you’ll learn
- Explain the purpose of object programs.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
sell creates a new object using spread and overrides stock. The original item still has stock 10, proving that the top-level object was not mutated.
Object spread is shallow. Mutating a nested object after spreading can still affect both versions.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
type Product = { id: number; name: string; stock: number };
function sell(product: Product, quantity: number): Product {
if (quantity < 0 || quantity > product.stock) throw new Error("Insufficient stock");
return { ...product, stock: product.stock - quantity };
}
const item: Product = { id: 2, name: "Notebook", stock: 10 };
console.log(sell(item, 3));
console.log(item.stock);Verified output
{ id: 2, name: 'Notebook', stock: 7 }
10Compile: npx tsc --strict --target es2022 --module commonjs object-programs.ts; run: node object-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
sell creates a new object using spread and overrides stock. The original item still has stock 10, proving that the top-level object was not mutated.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Common mistake and correction
Object spread is shallow. Mutating a nested object after spreading can still affect both versions.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Add a price field and write a function that applies a fixed discount without mutating the original.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Key idea to remember
sell creates a new object using spread and overrides stock. The original item still has stock 10, proving that the top-level object was not mutated.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.