Contact Management System
Contact Management System — purpose and prerequisites
A contact manager supports create, search, update, and delete operations with stable identifiers. A Map models a simple in-memory store and makes lookup by ID clear. For a real app, separate this service from persistent storage and validate contact details.
What you’ll learn
- Explain the purpose of contact management system.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
The service stores a copy of the record, returns a copy to callers, and deletes it by ID. After deletion the lookup yields undefined.
A Map exists only in process memory; restarting the program loses contacts unless persistence is added.
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 Contact = { id: number; name: string; phone: string };
class Contacts {
private readonly items = new Map<number, Contact>();
add(contact: Contact): void { this.items.set(contact.id, { ...contact }); }
find(id: number): Contact | undefined { const item = this.items.get(id); return item && { ...item }; }
remove(id: number): boolean { return this.items.delete(id); }
}
const book = new Contacts();
book.add({ id: 1, name: "Anika", phone: "555-0100" });
console.log(book.find(1));
console.log(book.remove(1), book.find(1));Verified output
{ id: 1, name: 'Anika', phone: '555-0100' }
true undefinedCompile: npx tsc --strict --target es2022 --module commonjs contact-management-system.ts; run: node contact-management-system.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
The service stores a copy of the record, returns a copy to callers, and deletes it by ID. After deletion the lookup yields undefined.
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
A Map exists only in process memory; restarting the program loses contacts unless persistence is added.
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
Implement updating a contact without changing its ID. Add JSON file persistence with atomic replacement.
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
The service stores a copy of the record, returns a copy to callers, and deletes it by ID. After deletion the lookup yields undefined.
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.