Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Module augmentation is a TypeScript compiler feature that allows developers to extend the type declarations of an existing, externally defined module without modifying its original source code. It leverages TypeScript’s declaration merging mechanism to inject new members into a module’s existing interfaces or namespaces during the type-checking phase. To perform module augmentation, the file containing the augmentation must itself be treated by TypeScript as a module. This means the file must contain at least one top-level import or export statement. The augmentation is then declared using the declare module syntax followed by the exact string literal of the target module’s specifier.

Syntax and Mechanics

// 1. The file must be a module (requires at least one import or export)
import { OriginalClass } from "target-library";

// 2. Declare the augmentation using the exact module specifier
declare module "target-library" {
  // 3. Inject new declarations to merge with existing interfaces
  export interface OriginalClass {
    newMethod(param: string): void;
    newProperty: number;
  }
}

Technical Constraints

  1. Module Context Requirement: If the augmenting file does not have any natural dependencies to import or export, you must force it into a module context using an empty export statement (export {}). If omitted, TypeScript treats the file as a global script, and the declare module block will create an ambient module declaration rather than augmenting the existing module.
  2. Declaration Merging Rules: Augmentation relies strictly on interface and namespace merging. You can add new members to an existing interface or namespace, but you cannot replace existing type signatures, and you cannot augment a type alias.
  3. Top-Level Limitations: You cannot declare new top-level declarations in the augmentation. You can only patch existing declarations (such as adding properties to an existing exported interface). You cannot add entirely new top-level value exports (like new functions or variables) because ES module exports are immutable at runtime, making it impossible to provide an implementation for a newly injected top-level value.
  4. Default Exports: You cannot augment a default export directly. You must identify and augment the underlying named declaration that the module exports as default.

Global Scope Augmentation

To augment the global scope (which TypeScript treats as a special ambient module) from within a local module, the declare global syntax is used. This applies the exact same declaration merging principles to the global namespace.
export {}; // Ensure the file is parsed as a module

declare global {
  // Merges with the globally defined String interface
  interface String {
    customStringMethod(): string;
  }
}
Master TypeScript with Deep Grasping Methodology!Learn More