Augmentations for the global scope can only be directly nested in external modules or ambient module declarations(2669)
Asked Answered
E

3

220

I would like to store my NodeJS config in the global scope.

I tried to follow this => Extending TypeScript Global object in node.js and other solution on stackoverflow,

I made a file called global.d.ts where I have the following code

declare global {
    namespace NodeJS {
      interface Global {
          config: MyConfigType
      }
    }
  }

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

but doing this works fine =>

declare module NodeJS  {
    interface Global {
        config: MyConfigType
    }
}

the problem is, I need to import the file MyConfigType to type the config, but the second option do not allow that.

Estren answered 21/7, 2019 at 10:44 Comment(3)
Understand that an "external module" is a file containing an import or export statement, that an "ambient module declaration" reads declare module "m" {} (note the quotes), and reread the error message.Echoism
You might need export {}Eward
In an ambient decl. file that's not been turned into a module you are already operating in the global scope (outside of declare module {} braces) so you can just omit declare globalSayyid
N
444

You can indicate that the file is a module like so:

export {};

declare global {
    namespace NodeJS {
        interface Global {
            config: MyConfigType
        }
    }
}
Nonscheduled answered 27/12, 2019 at 11:10 Comment(4)
wow, works! What trickery is this "export {};". Can you provide some information that I can read up on as to why that works?Mastoidectomy
I think the trick is just that an "external module" is a file containing an import or export statement, so this makes it an "external module". It does seem a little strange that this is needed though...Pinette
@Mastoidectomy here's a good explanation of why https://mcmap.net/q/120615/-how-to-configure-custom-global-interfaces-d-ts-files-for-typescriptFrankforter
I think that declaring MyConfigType in another file and then using import should fix the issue too. (if this option is applicable to your case)Pack
T
47

Or if you're trying to add a global type within the browser context:

export {};

declare global {
  interface Window {
    ENV: any;
  }
}
Tutti answered 7/12, 2020 at 23:39 Comment(2)
Hmmm. Just tried this but every time I close the .d.ts file the variable that's on the window is underlined in red as an error. The error goes away when I open the window.Cental
@Cental try adding the file to tsconfig.json: "include": ["globals.d.ts"]Tsan
D
18

If your .d.ts is executing as a "script" (it has no import/export statements), you won't need to declare global at all, and can simply remove it. A "script" is already executing in the global context.

(aside: this is why adding export {} works, it turns it from a "script" into a "module" and your declarations in the .d.ts are no longer global by default)

With an example modifying console:

Before:

declare global {
  interface Console {
    log2: (...args: any[])=>void;
  }
}

After:

// This will be global!
interface Console {
  log2: (...args: any[])=>void;
}

Thanks to @okcoker and https://mcmap.net/q/120615/-how-to-configure-custom-global-interfaces-d-ts-files-for-typescript for the explanation of .d.ts contexts

Dillman answered 12/5, 2023 at 5:52 Comment(1)
I am using TypeScript within a browser app, with .js scripts loaded via <script> tags in index.js. This works perfectly for my use case.Sonia

© 2022 - 2024 — McMap. All rights reserved.