← Back to TypeScript Course | Chapter 11: Type Declarations and DefinitelyTyped | Lesson 4 of 5

Module Augmentation की जानकारी

Module augmentation आपको एक मौजूदा module की original declaration file बदले बिना इसमें type information जोड़ने देता है। यह उपयोगी है जब plugins या custom code एक library की API extend करते हैं।
Syntax
typescript
declare module "module-name" {
  interface ExistingInterface {
    newProperty: type;
  }
}

Module Augmentation समझना

Module augmentation आपको अपने own code से एक मौजूदा module के exported types फिर से खोलने और इनमें नए members जोड़ने देता है, जो उपयोगी है जब एक library की types आपको चाहिए कुछ चीज़ miss कर रही हों बिना library खुद को fork किए।

उदाहरण: Understanding Module Augmentation

typescript
// Reopening an existing module's types to add a member:
declare module "existing-lib" {
  export interface Config {
    customOption?: boolean;
  }
}
console.log("Module augmentation adds to a library's existing types");

एक Interface Augment करना

एक module द्वारा export की गई interface को augment करने का मतलब है उसी interface name को एक declare module block के अंदर declare करना; TypeScript आपकी additional properties को original interface को replace करने के बजाय इसमें merge करता है।

उदाहरण: Augmenting an Interface

typescript
declare module "existing-lib" {
  interface Config {
    timeout: number;
  }
}
// TypeScript merges this into the original Config interface.
console.log("Augmented interface merges rather than replaces");

एक Class API Augment करना

एक class का exported API augment करना एक interface जैसे ही तरीके से काम करता है — आप module declaration के अंदर नए method या property signatures जोड़ते हैं, और TypeScript class को ऐसे treat करता है जैसे इसके पास हमेशा से ये थे।

उदाहरण: Augmenting a Class API

typescript
declare module "existing-lib" {
  interface Client {
    retryCount: number;
  }
}
console.log("Class API augmented with an additional property");

Runtime Implementation

Module augmentation सिर्फ यह change करता है कि TypeScript क्या मानता है exist करता है — यह zero runtime behavior जोड़ता है, तो जो भी property या method आप augment करते हैं वह runtime पर पहले से actually exist करना चाहिए, या इसे call करना fail होगा।

उदाहरण: Runtime Implementation

typescript
// Augmentation only changes what TypeScript believes exists.
// The property must ALREADY exist at runtime or calling it fails:
declare module "existing-lib" {
  interface Client {
    debugMode: boolean;
  }
}
console.log("Augmentation adds zero runtime behavior on its own");

Module Augmentation कब उपयोग करें

Module augmentation तब उपयोग करें जब एक third-party library की published types आपके use case के लिए incomplete या थोड़ी गलत हों; बाकी सब के लिए, library को अपने own code में extend या wrap करना आमतौर पर एक safer, less fragile choice है।

उदाहरण: When to Use Module Augmentation

typescript
// Use when a library's published types are incomplete or wrong.
// For everything else, wrapping the library in your own code is safer.
declare module "existing-lib" {
  interface Config {
    experimentalFlag?: boolean;
  }
}
console.log("Reach for augmentation only when types are genuinely incomplete");
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. एक module को एक plain top-level interface से augment करना, जब इसे declare module "name" के अंदर होना चाहिए और file एक module होनी चाहिए (एक import या export रखे)।
  2. Augmentation से एक method जोड़ना और इसके runtime पर exist करने की उम्मीद करना, जब यह कोई code नहीं जोड़ता।
  3. declare module में एक गलत module name उपयोग करना, जो मौजूदा module में merge होने के बजाय एक नया module create करता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.