• Error
  • Reported by the binder

TS2669

Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.

declare global appears in a file that isn't a module. Add an import or export to the file, or declare the globals at the top level of a .d.ts.

What it means

declare global { ... } is only allowed directly inside a module (a file with a top-level import or export) or inside an ambient module declaration. In a script file, top-level declarations are already global, so the block is rejected.

Example

index.ts
declare global {
  interface Window { appVersion: string }
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(1,9): error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.

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

  • Make the file a module, for example by adding export {};.
  • Or remove the declare global wrapper and declare the names at the top level of a script .d.ts file.