Project: A Typed Shopping Cart
Let's put it together: a small cart with products, coupons and a bill. Every part is typed, so mistakes show up while you type.
This project shows how types guide a real design: products, cart lines and coupons each have a clear shape, and the compiler catches mistakes as the code grows. Try adding a feature and notice how TypeScript points you to every place that needs a change.
Example
Try improving it
- Add a
remove(id: number)method - Add a
"freeShipping"coupon kind - TypeScript will show you the switch that needs updating - Move the types into
types.tsand import them - Write the bill as a nice table with
console.table
Next steps: use TypeScript in a React or Angular project, or on the server with Node.js + Express - see those courses.
Key points
- Model your data with interfaces and unions first.
- Let the compiler find what to update when things change.
- Records and discriminated unions keep rules complete.
📝 Test yourself
1. Why does add() return this?
2. If you add a new Coupon kind, what helps you find code to update?
3. Fill in the type that maps every category to a rate:
const GST_RATE: <Category, number> = { ... };
