🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In TypeScript, understanding the difference between namespaces and modules is key to organizing code effectively. A namespace is a TypeScript-specific way to group related code. It can be used to organize code into logical groups and avoid name collisions. A module, on the other hand, is the standard TypeScript and ES6 way to divide code into separate files, each of which can export variables, functions, or types.
2. Key Points
1. Scope: Namespaces are global in scope, while modules are file-based.
2. Declaration: Namespaces are declared with the namespace keyword, modules use export and import.
3. File Structure: Namespaces can be in one or multiple files, while modules correspond to files.
4. Use in Modern Applications: Modules are preferred in modern TypeScript applications due to better tooling and compatibility with the ES6 standard.
3. Differences
| Characteristic | Namespace | Module |
|---|---|---|
| Scope | Global | File-based |
| Declaration | With namespace keyword | With export and import |
| File Structure | Can be in one or multiple files | One module per file |
| Use in Modern Applications | Less common | Preferred for compatibility with ES6 |
4. Example
// Example of Namespace
namespace MyNamespace {
export class MyClass {
myMethod() {}
}
}
// Example of Module
// In file myModule.ts
export class MyModuleClass {
moduleMethod() {}
}
// In another file
import { MyModuleClass } from './myModule';
Output:
No direct output as these are structural code examples.
Explanation:
1. MyNamespace is a namespace that groups MyClass under a common name. It can be accessed globally if properly referenced.
2. MyModuleClass is part of a module, which is imported from myModule.ts. Each module is distinct and manages its own dependencies through imports and exports.
5. When to use?
- Use namespaces in TypeScript if you are working on a legacy codebase or for simple applications where you want to group related functionality.
- Use modules for modern TypeScript development, especially when building large-scale applications or when you need to ensure compatibility with JavaScript ES6.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment