TypeScript Modules and Namespaces in 2025: ES Modules, Import/Export, Default vs Named, and Best Practices
1. What are modules and namespaces in TypeScript?
Q: What is the difference between modules and namespaces?
Modules (especially ES Modules) are the modern, file-based way to organize and share code using import and export. They align with JavaScript standards and are recommended for all new projects.
Namespaces are a legacy TypeScript feature (formerly "internal modules") for grouping code logically within the global scope using the namespace keyword.
Key Difference: Modules are preferred in 2025 – namespaces are mainly for legacy code or simple scripts.
2. What are ES Modules in TypeScript, and how do import and export work?
Q: How do ES Modules work in TypeScript?
ES Modules use standard import/export syntax and are configured with "module": "ESNext" or "NodeNext" in tsconfig.json.
Example code remains excellent – see original for mathUtils.ts and main.ts.
3. What are default exports vs. named exports in TypeScript?
Q: Default vs named exports – which to use?
Use named exports for multiple related items; default for a single primary export (e.g., a main class).
Excellent example with calculator.ts – no changes needed.
4. What are namespaces in TypeScript, and when to use them?
Q: Are namespaces still relevant in 2025?
Namespaces are mostly legacy. Use them only for small scripts or migrating old code. Modern projects should use ES modules.
Example with utils.ts remains valid for illustration.
5. Comprehensive example combining modules and namespaces
Great real-world demo of interoperability (exporting a namespace from a module). Content unchanged – highly useful for migration scenarios.
6. What are common mistakes in TypeScript modules and namespaces?
Q: What pitfalls to avoid?
- Using namespaces in new projects instead of ES modules
- Incorrect import paths or missing extensions in NodeNext mode
- Mixing module systems inconsistently
- Forgetting to export items from namespaces
7. What are best practices for TypeScript modules and namespaces in 2025?
Q: Modern best practices?
- Always prefer ES modules – they are the standard.
- Use named exports for utilities, default for primary classes/components.
- Set
"module": "ESNext"or"NodeNext"and"moduleResolution": "NodeNext"or"Bundler". - Avoid namespaces unless maintaining legacy code.
- Enable
"strict": trueand use consistent file naming.
Frequently asked questions
What are modules and namespaces in TypeScript?
Modules (ES modules) are file-based code organization using import/export. Namespaces are TypeScript-specific logical groupings using the namespace keyword, mainly for legacy or simple scripts.
What are ES Modules in TypeScript and how do import/export work?
ES Modules are the modern standard using import and export statements. Named exports for multiple items, default for a primary export. Configured via 'module': 'ESNext' in tsconfig.json.
What is the difference between default exports and named exports?
Default exports allow flexible import names (one per module). Named exports require exact names (or aliases) and support multiple per module.
When should you use namespaces in TypeScript?
Namespaces are best for legacy code, small scripts, or when ES modules aren't feasible. In modern projects, prefer ES modules for scalability and tooling support.
Can modules and namespaces be used together?
Yes – namespaces can be exported as part of ES modules for interoperability, especially during migration.
What are common mistakes with TypeScript modules and namespaces?
Overusing namespaces in modern projects, incorrect paths, mixing export styles inconsistently, forgetting module settings in tsconfig.json.
What are best practices for TypeScript modules in 2025?
Always prefer ES modules. Use named exports for utilities, default for primary classes. Set 'module': 'ESNext' or 'NodeNext', enable strict mode.