Author : HASSAN MD TAREQ
- Modules are executed within their own scope, not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms
- To consume a variable, function, class, interface, etc. exported from a different module, it has to be imported using one of the import forms
- In TypeScript (as in ECMAScript 2015) any file containing a top-level import or export is considered a module. A file without any top-level import or export declarations is treated as a script whose contents are available in the global scope
- Modules import one another using a module loader. At runtime the module loader is responsible for locating and executing all dependencies of a module before executing it
Module vs Namespace
A module in TypeScript is a standard ES6 notion, it uses import / export keywords at the top level of the code. A namespace is a notion specific to TypeScript to help to organize the code in an obsolete fashion.
Namespaces are used to organize/encapsulate your code. External modules are used to organize/encapsulate your code AND to locate your code at runtime.