TypeScript Modules and Namespaces
Understand the difference between modules and namespaces, and learn how to use them to organize and structure your code effectively.
TypeScript Modules and Namespaces is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Modules and Namespaces
Modules and namespaces in TypeScript help organize and structure your code, especially in large projects.
While both achieve similar goals, they are used differently, and this lesson will guide you through their distinctions and usage.

2
What are Modules?
Modules are files or pieces of code that export functionality, such as variables, functions, or classes, to be reused in other parts of an application.
TypeScript uses ES modules for import/export functionality, which are now a standard in JavaScript.
3
Exporting and Importing in Modules
You can export code from a module using export and import it using import:
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
Import it in another file:
// app.ts
import { add } from './math';
console.log(add(2, 3));// math.ts
export function add(a: number, b: number): number {
return a + b;
}
// app.ts
import { add } from './math';
console.log(add(2, 3));4
Default Exports
TypeScript also allows default exports, which let you export a single value from a module:
// math.ts
export default function add(a: number, b: number): number {
return a + b;
}
Import it without curly braces:
// app.ts
import add from './math';
console.log(add(2, 3));// math.ts
export default function add(a: number, b: number): number {
return a + b;
}
// app.ts
import add from './math';
console.log(add(2, 3));5
What are Namespaces?
Namespaces in TypeScript are a way to group related code together under a single name, providing a way to organize and avoid naming conflicts.
Namespaces are defined using the namespace keyword.
6
Creating and Using Namespaces
Here is an example of a namespace:
namespace MathUtils {
export function add(a: number, b: number): number {
return a + b;
}
}
You can use it like this:
console.log(MathUtils.add(2, 3));namespace MathUtils {
export function add(a: number, b: number): number {
return a + b;
}
}
console.log(MathUtils.add(2, 3));7
Modules vs. Namespaces
Here are the key differences between modules and namespaces:
- Modules are file-based and support ES module syntax.
- Namespaces are ideal for organizing code in a single file but are considered outdated for modern applications.
8
9
Best Practices
When working with TypeScript:
- Use modules for new projects, as they align with modern JavaScript standards.
- Consider namespaces only for legacy or small-scale projects.
- Always keep your code organized and consistent.
10
Congratulations!
You’ve learned how to use modules and namespaces to structure and organize your TypeScript projects. Keep practicing to master these important concepts!

Frequently asked questions
Is the “TypeScript Modules and Namespaces” lesson free?
Yes — the full text of “TypeScript Modules and Namespaces” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “TypeScript Modules and Namespaces”?
Understand the difference between modules and namespaces, and learn how to use them to organize and structure your code effectively. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “TypeScript Modules and Namespaces” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Type Inference and Contextual Typing
- Decorators in TypeScript
- TypeScript Modules and Namespaces