My next step from the Typescript overview is to see the differences between namespaces and modules. Both of them can be declared easily:

namespace X {
}

module Y{
}

There is a general explanation on how to use them on the TypeScript’s website. My opinion is that we need to learn the differences between them before using them. If we look into history, we would find that namespaces were initially named internal modules. Both namespaces and modules can help to organize your code. The main difference is the way they can organize it. For example, namespaces cannot contain requirements and must be added separately.

Common between namespaces and modules:

  • Cоde organization
  • Contain code and declarations

Modules provide some benefits:

  • Declare their dependencies
  • Better code reuse
  • Strong isolation
  • Have a dependency on the module loader

Conclusion:

Recently there is a push towards using modules for almost everything. Namespaces are preferred for quick demo applications or code examples.

Was this article helpful?