0

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 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
No, modules are singletons, so if it was already imported somewhere it will return the existing module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.