I was looking for differences in Namespaces and Modules in TypeScript and come across one point that modules are declarative while namespaces are not. Also, modules can be imported in any order while namespaces can't. So, what does 'declarativeness' of modules means and how they work even when imported in random order?
1 Answer
You can view namespaces as just wrapper objects around other objects, so the declaration order still matters.
Modules require extra code to make them even work.
This extra code is created in the window by your module bundler (webpack for example) and it holds references to all your modules. You can inspect it by opening the JS file generated by webpack.
The order of your modules is not important because they all get registered when you compile your bundle.
2 Comments
Gourav Pokharkar
But, what if one module is dependant on another. Does dependee module get imported twice, once with its own code and once while explicitly importing in our own code?
Kokodoko
No, modules are singletons, so if it was already imported somewhere it will return the existing module