• Error
  • Reported by the binder

TS2435

Ambient modules cannot be nested in other modules or namespaces.

A declare module "name" block appears inside another module or namespace. Move it to the top level of a declaration file.

What it means

Ambient module declarations (declare module "some-package" { ... }) describe a whole module by its name, so they must be at the top level of a file. Nested inside a namespace or another module declaration they're reported.

Example

index.ts
export namespace Vendor {
  declare module "vendor-lib" {
    export const version: string;
  }
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(2,18): error TS2435: Ambient modules cannot be nested in other modules or namespaces.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Move the declare module "..." block to the top level of a .d.ts file.
  • To add to a module from inside a file that is itself a module, write declare module "name" at that file's top level (a module augmentation).