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

Global Augmentation की जानकारी

Global augmentation एक module को Window या built-in prototypes जैसे global types में declarations जोड़ने देता है। यह उपयोगी है जब external scripts या application code global APIs जोड़ते हैं।
Syntax
typescript
declare global {
  interface Window {
    newProperty: type;
  }
}
export {};

Global Augmentation समझना

Global augmentation आपको उन types में नए members जोड़ने देता है जो आपके program में हर जगह बिना किसी import के available हैं — सबसे common तौर पर built-in Window या globalThis objects — एक declare global block उपयोग करते हुए।

उदाहरण: Understanding Global Augmentation

typescript
declare global {
  interface Window {
    myGlobalFlag: boolean;
  }
}
console.log("declare global adds members available everywhere, no import needed");
export {};

Window Extend करना

Window extend करना एक global variable को type करने का standard तरीका है जिसे कोई और script browser के window object से attach करती है, जैसे आपके bundle run होने से पहले एक <script> tag से loaded एक analytics SDK।

उदाहरण: Extending Window

typescript
declare global {
  interface Window {
    analytics: { track(event: string): void };
  }
}
console.log("Window.analytics typed for a script-tag-loaded SDK");
export {};

Global Interfaces Extend करना

आप Window से आगे दूसरी ambient global interfaces भी augment कर सकते हैं, जैसे एक Node environment में globalThis खुद, process-wide globals describe करने के लिए जिन पर आपका code rely करता है लेकिन import नहीं करता।

उदाहरण: Extending Global Interfaces

typescript
declare global {
  interface globalThis {
    appConfig: { env: string };
  }
}
console.log("globalThis extended for a Node-style global");
export {};

Global Methods जोड़ना

एक global method (जैसे एक polyfill जिसके मौजूद होने की आपको जानकारी है, जैसे structuredClone) global interface में जोड़ना आपको इसे call करने देता है बिना TypeScript complain किए कि method उस built-in type पर exist नहीं करता।

उदाहरण: Adding Global Methods

typescript
declare global {
  interface Window {
    structuredClone<T>(value: T): T;
  }
}
console.log("Polyfilled global method typed via declare global");
export {};

Best Practices

चूंकि global augmentations पूरे program के type space को एक साथ affect करते हैं, इन्हें narrow और well-documented रखें, और global scope reach करने के बजाय जहां भी actually possible हो scoped module augmentation prefer करें।

उदाहरण: Best Practices

typescript
// Keep global augmentations narrow and documented;
// prefer module augmentation when the scope isn't truly global.
declare global {
  interface Window {
    __APP_VERSION__: string;
  }
}
console.log("Narrow, well-documented global augmentation");
export {};
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. export {} या एक import भूल जाना, तो declare global allowed नहीं है क्योंकि file module नहीं है।
  2. value को actually define किए बिना window.analytics के types declare करना, एक runtime error देते हुए।
  3. एक मौजूदा Window property को एक अलग type के साथ redeclare करना, जो conflict करता है।
चैप्टर सारांश
  • .d.ts declaration files बिना इसे बदले मौजूदा JavaScript code के types describe करती हैं।
  • आप अपनी खुद की declaration files लिख सकते हैं या @types packages से ready-made वाली install कर सकते हैं।
  • Module और global augmentation libraries या global scope से मौजूदा types extend करते हैं।
🔒

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.