We briefly looked at the use of modules when we covered classes in Chapter 1, Efficient Implementation of Basic Data Structures and Algorithms. Let's take a closer look at namespaces and modules now.
A little theory first. Since TypeScript 1.5, namespaces is the preferred nomenclature for what were known earlier as internal modules. Namespacing is a way for us to logically organize a piece of functionality or declaration in its unique namespace. The other advantage to this organization is that the names declared within this space do not collide with identical names elsewhere in your source code. Let's take a look at an example. Consider building a very simplistic cafe for a workplace. It should basically support three functions: pay
, makeSelection
, and dispense
. The implementations of these, as you can imagine, must be straightforward and have been abstracted as that's not the main point of understanding namespaces.
Let's define the classes and interfaces under the namespace...