How do you use TypeScript's Namespaces and Modules to organize code?
Instruction: Discuss the differences between Namespaces and Modules in TypeScript and provide examples on when to use each.
Context: This question assesses the candidate's knowledge of code organization in TypeScript, particularly the use of Namespaces and Modules, and their ability to apply these concepts in a scalable application structure.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
A module has its own scope and exports an API for other modules to import. Prefer this explicit dependency model for most application code, whether the application is small or large. TypeScript checks it and emits code according to the selected module/runtime settings.
A namespace groups names and can emit a runtime object for value-bearing declarations...