← Back to TypeScript Course | Chapter 9: Modules and Namespaces | Lesson 4 of 7

Namespace के Basics

एक namespace related TypeScript declarations को एक single name के under group करता है। Namespaces मुख्य रूप से एक global या legacy-style codebase के अंदर code organize करने के लिए उपयोगी हैं, जबकि modern projects आमतौर पर ES modules पसंद करते हैं।
Syntax
typescript
namespace NamespaceName {
  export const value = expression;
  export function name(): type {}
}
NamespaceName.name();

एक Namespace बनाना

Related declarations के लिए एक named container बनाने के लिए namespace keyword उपयोग करें, उन्हें separate files या एक ES module system की ज़रूरत के बिना एक single accessible name के under group करते हुए।

उदाहरण: Creating a Namespace

typescript
namespace Shapes {
  export function area(side: number) {
    return side * side;
  }
}
console.log(Shapes.area(4));

Namespace Members Export करना

Namespace के अंदर members को इससे export किया जाना चाहिए अगर namespace के बाहर का code इन्हें access करना चाहे, यह mirror करते हुए कि modules अपनी own boundary के बाहर क्या visible है control करते हैं।

उदाहरण: Exporting Namespace Members

typescript
namespace MathUtils {
  export const PI = 3.14159;
  function internalHelper() { return "hidden"; }
}
console.log(MathUtils.PI);

Nested Namespaces

Namespaces दूसरे namespaces रख सकते हैं, related declarations को deeper, more specific groupings में nest करते हुए, similar कि कैसे packages दूसरे packages के अंदर nested हो सकते हैं। Deeply nested namespaces dotted paths से access किए जाते हैं, जैसे Outer.Inner.member।

उदाहरण: Nested Namespaces

typescript
namespace Outer {
  export namespace Inner {
    export const value = 42;
  }
}
console.log(Outer.Inner.value);

Classes के साथ Namespaces

Classes namespaces से export की जा सकती हैं और फिर namespace name के through access की जा सकती हैं, एक namespace को related classes, interfaces, और functions को एक umbrella के under bundle करने देते हुए।

उदाहरण: Namespaces with Classes

typescript
namespace Shapes {
  export class Circle {
    constructor(public radius: number) {}
  }
}
const c = new Shapes.Circle(5);
console.log(c.radius);

Namespaces and Modern Modules

Namespaces एक older TypeScript organization mechanism है जो widespread ES module support से पहले का है; modern applications के लिए, ES modules code organize करने का recommended तरीका हैं।

उदाहरण: Namespaces and Modern Modules

typescript
// Older style:
namespace Legacy {
  export const value = 1;
}
// Modern style (recommended):
// export const value = 1;
console.log(Legacy.value);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक namespace member पर export भूल जाना और फिर बाहर MathUtils.internalHelper उपयोग करते समय एक error पाना।
  2. एक file में namespace को import/export के साथ mix करना, तो namespace global होने के बजाय एक module के लिए local बन जाता है।
  3. नए code में namespaces उपयोग करना जब ES modules preferred तरीका हैं, और यह देखकर surprised होना कि वे runtime पर एक object emit करते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.