Module 29: Modules, Classes, Generics and Utility Types
Organising a project
🏠The everyday picture
A well-run office: departments by subject, a reception that directs visitors, and a manual on the front desk explaining where everything is.
📘What it really is
- A typical small TypeScript project:
inventory/
src/
types.ts shared types
data.ts sample or stored data
inventory.ts stock logic
expenses.ts expense logic
utils.ts small general helpers
app.ts starts the program
tests/
inventory.test.ts
package.json
tsconfig.json
README.md
.gitignore
- Principles:
- one subject per file: everything about expenses in
expenses.ts; - logic separate from input and output: calculation files never print or save;
app.tsdoes that (pure functions again, Module 23); - dependencies point one way:
app.tsimports from the others, never the reverse; -
names say what's inside, so you (and AI) can find things.
-
Tests can be written in TypeScript too:
inventory.test.ts, run withnode --test. Set"test"tonode --test "tests/**/*.test.ts"inpackage.json. -
package.jsonscripts become the project's controls:npm start,npm test,npm run check. - When working with AI on a project, tell it the structure and which file to change. Small, well-named files make AI's changes easier to check, one diff at a time (Builder Path, Lessons 4 and 5).
🖱️Try it
Reorganise your inventory project into the structure above. Add "start": "node src/app.ts" to the scripts,
and move or write at least three tests into tests/. Check that npm start, npm test and npm run check all
work.
✅Quick check
Why should calculation files never print or save?
Show the answer
Keeping logic pure makes it easy to test and reuse; input and output stay in one place.
inventory.ts imports from app.ts, and app.ts imports from inventory.ts. What's wrong?
Show the answer
A circular dependency. Dependencies should point one way, with app.ts at the top.
