Skip to content

๐Ÿ—‚๏ธ 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.md

Youโ€™ll use:

  • main.ts: your entry point
  • utils/: for reusable logic
  • types/: for interfaces and types
  • README.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;
}

๐Ÿง  export makes 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 type is optional, but helps with clarity.


๐Ÿ’ก Naming conventions (recap) โ€‹

File / FolderPurpose
main.tsEntry point of the app
utils/Reusable functions
types/All your interfaces/types
README.mdProject description

๐Ÿ“˜ Learn more: TypeScript Modules


๐Ÿงช Try it out โ€‹

If you used tsx:

bash
tsx main.ts

With 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 tools
  • api/ โ€” for business logic
  • config/, 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.

๐Ÿ‘‰ Go to Chapter 6: Introduction to Git and GitHub

Publiรฉ sous licence MIT