Skip to main content
The export as namespace syntax in TypeScript is a module-level declaration that exposes a module’s exports to the global scope under a single specified identifier. It provides ambient type information for Universal Module Definition (UMD) libraries, allowing the TypeScript compiler to recognize the module’s members as global variables when a module loader is not present. Syntax
Mechanics and Compiler Behavior
  • Type-Level Binding: The declaration does not emit runtime JavaScript to attach the module to the global object (e.g., window or global). It strictly informs the TypeScript type checker that the underlying UMD bundle will handle the global assignment at runtime.
  • Module Requirement: It can only be declared inside a file that is already recognized as a module by the compiler (i.e., a file containing at least one top-level import or export statement).
  • Single Declaration Limit: TypeScript strictly allows only one export as namespace declaration per file. Attempting to declare multiple namespace aliases within the same module will result in a compiler error.
  • Global Access Resolution: By default, the declared global namespace identifier is only accessible in script files. If the consuming file is a module (meaning it contains top-level import or export statements), TypeScript intentionally hides the global alias and throws an error (TS2686), enforcing explicit imports. However, this strict isolation can be bypassed by enabling the allowUmdGlobalAccess compiler option (introduced in TypeScript 3.5), which explicitly permits module files to access UMD globals without requiring an import statement.
Implementation Example
When the above declaration is included in a TypeScript compilation context, the compiler’s resolution behavior depends on the nature of the consuming file and the tsconfig.json configuration: 1. Consumption in a Script File
2. Consumption in a Module File
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More