12.2 Ambient Module Declarations

An AmbientModuleDeclaration declares a module. This type of declaration is permitted only at the top level in a source file that contributes to the global namespace (section 11.1). The StringLiteral must specify a top-level module name. Relative module names are not permitted.

  AmbientModuleDeclaration:   declaremoduleStringLiteral{DeclarationModule}

An ImportRequireDeclaration in an AmbientModuleDeclaration may reference other modules only through top-level module names. Relative module names are not permitted.

If an ambient module declaration includes an export assignment, it is an error for any of the declarations within the module to specify an export modifier. If an ambient module declaration contains no export assignment, entities declared in the module are exported regardless of whether their declarations include the optional export modifier.

Ambient modules are “open-ended” and ambient module declarations with the same string literal name contribute to a single module. For example, the following two declarations of a module ‘io’ might be located in separate source files.

  1. declare module "io" {
  2. export function readFile(filename: string): string;
  3. }
  4. declare module "io" {
  5. export function writeFile(filename: string, data: string): void;
  6. }

This has the same effect as a single combined declaration:

  1. declare module "io" {
  2. export function readFile(filename: string): string;
  3. export function writeFile(filename: string, data: string): void;
  4. }