TypeScript: Module System
Employing “Namespaces” in TypeScript to encapsulate your data
In this lesson, we are going to learn about the precursor of the ECMAScript module implemented purely in TypeScript. These are called namespaces and they are quite fascinating.
In the previous lesson, we learn about the standard module system used by TypeScript which also coincides with the standard module system of JavaScript standardized by the ECMAScript.
When the module
compiler-option (or the --module
flag) is set to CommonJS
, the TypeScript compiler converts the ECMAScript module statements such as import
and export
to equivalent require
and exports
statements for Node
. Else, it is left intact so that it can work inside browsers that support native ECMAScript modules.
The benefit of using a module system in a project is that you can split reusable logic and application logic between multiple files. However, this also means your runtime environment must support one of these module systems.
If you want the compiled JavaScript program to run on Node
, you can set the module
compiler-option to CommonJS
. If you want to run the program in a browser environment, you can use ES2015
or…