Modules & NamespacesIntermediate
1Question 1 of 1
typescriptWhat is the difference between ES Modules and Namespaces in TypeScript?
Explanation & Concept
• Namespaces: Legacy TypeScript feature (`namespace MySpace { ... }`) designed before ES6 had a module standard. They pollute global scope or require complex bundling flags. • ES Modules: The standard (`import`/`export`). Code is isolated per file. Always prefer ES Modules in modern TypeScript projects.
Code Example
typescript
// Prefer ES Modules
export interface ApiResponse<T> {
data: T;
statusCode: number;
}
export function formatResponse<T>(data: T): ApiResponse<T> {
return { data, statusCode: 200 };
}How It Works & Key Takeaways
Standard ES Modules enable tree-shaking, static analysis, and direct interoperability with modern bundlers and runtime engines.
Mock Test · 1 Questions · Optional
Ready for tsconfig Deep Dive?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions