Appearance
๐๏ธ Chapter 5 โ Structuring a TypeScript Project โ
Now that you know how to use TypeScript, itโs time to organize your code like a real developer.
A good structure helps you:
- stay focused,
- avoid messy folders,
- and work in teams more easily.
Letโs build a clean mini-project structure.
๐๏ธ Folder structure (basic version) โ
bash
my-app/
โโโ main.ts
โโโ utils/
โ โโโ math.ts
โโโ types/
โ โโโ index.ts
โโโ README.mdYouโll use:
main.ts: your entry pointutils/: for reusable logictypes/: for interfaces and typesREADME.md: to describe your project
Letโs build it step by step.
๐ Step 1 โ Create the project folder โ
bash
mkdir my-app
cd my-app
code .๐ Step 2 โ Create a simple main.ts โ
ts
// main.ts
import { add } from "./utils/math.ts";
console.log(add(2, 3));๐ง Step 3 โ Create a utils/math.ts file โ
ts
// utils/math.ts
export function add(a: number, b: number): number {
return a + b;
}๐ง
exportmakes the function available in other files.
๐งฑ Step 4 โ Add some types โ
Create types/index.ts:
ts
// types/index.ts
export interface User {
name: string;
age: number;
isAdmin?: boolean; // optional
}Then use it in main.ts:
ts
import type { User } from "./types/index.ts";
const bob: User = {
name: "Bob",
age: 42,
};
console.log(bob);๐ง
import typeis optional, but helps with clarity.
๐ก Naming conventions (recap) โ
| File / Folder | Purpose |
|---|---|
main.ts | Entry point of the app |
utils/ | Reusable functions |
types/ | All your interfaces/types |
README.md | Project description |
๐ Learn more: TypeScript Modules
๐งช Try it out โ
If you used tsx:
bash
tsx main.tsWith Deno:
bash
deno run --allow-read main.tsโ You should see the result of the addition and the user object.
๐ฆ Going further โ
In larger projects, you will see:
lib/โ for technical toolsapi/โ for business logicconfig/,tests/,bin/โ for configuration, testing, or scripts
These folders help split your code by function, not just by type.
๐ Check out:
โ Whatโs next? โ
In the next chapter, weโll learn how to use Git and GitHub to save your work, collaborate with others, and share your code online.