Level 3 · Programming Skills · Course 3.2 TypeScript
Module 28: Interfaces and Type Design
Goal: describe your app's data with named types and interfaces, and design types that make mistakes impossible to write, not just easy to spot.
You'll need: your inventory TypeScript project from Module 27.
Lessons
- 1Interfaces15–25 minutes✓
- 2Optional and readonly properties15–25 minutes✓
- 3Type aliases and when to use which15–25 minutes✓
- 4Discriminated unions15–25 minutes✓
- 5Designing types for a real app15–25 minutes✓
🛠️Module practice: Project D, part 2, the inventory model
- Complete
types.tsfrom Lesson 5. - Create sample data in
data.tsfor at least six products, two suppliers, ten stock movements and five expenses, fully typed. - Write
currentStock(productId, movements)that works out stock from the movements, using aswitchon the movement type. npm run checkpasses with noany. Commit.
✅Module check
Explain in a few sentences, in your own words:
How do interfaces and discriminated unions help you design an app's data, and what does "make impossible states impossible" mean?
Show a good answer
A good answer mentions: interfaces (or object type aliases) name the shape of each thing in the app, so missing or misspelt properties are caught, with optional and readonly marking what may be absent or must never change. A discriminated union uses a shared literal property to describe each kind or stage of something with exactly the data it needs, and TypeScript checks every case is handled. Designing types so contradictory combinations (like "unpaid with a payment date") can't be written prevents bugs before any code exists.
📝Module quiz
10 questions · 10 marks each · 100 marks in all · pass mark 70% · retry as often as you like ·
