Module Augmentation की जानकारी
In this page:
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
// 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
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
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
// 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
// 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");
- एक module को एक plain top-level
interfaceसे augment करना, जब इसेdeclare module "name"के अंदर होना चाहिए और file एक module होनी चाहिए (एकimportयाexportरखे)। - Augmentation से एक method जोड़ना और इसके runtime पर exist करने की उम्मीद करना, जब यह कोई code नहीं जोड़ता।
declare moduleमें एक गलत module name उपयोग करना, जो मौजूदा module में merge होने के बजाय एक नया module create करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: