Modules are a design pattern used to improve the structure, readability, and testability of code. Many programming languages have some level of support for modules. Of course, TypeScript is one of them!
Modules are incredibly useful; thanks to them, you can do the following:
- Structure code in self-contained blocks (just like sections in a document or chapters in a book).
- Encapsulate code (since modules are self-contained).
- Define public APIs: Modules expose what they want to define their interface with the outside world.
- Create isolated namespaces: Modules have their own namespaces and they don't pollute other modules.
- Reuse existing code by loading and using third-party libraries, other modules, and much more.