Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add variable to window

Tags:

typescript

I`ve added

window.d.ts file with next content as proposed by many answers here

interface Window {
    gameManager?: any;
}

but still getting Property 'gameManager' does not exist on type 'Window'

Do you have any idea how to simply add variable to window object without so much pain?

like image 579
Alex G. Avatar asked Sep 01 '26 08:09

Alex G.


1 Answers

So, if you do, as often recommended:

declare global {
  interface Window {
    CONFIG: any
  }
}

then you will get the TS2669 error: "Augmentations for the global scope can only be directly nested in external modules or ambient module declarations."

This is confusing, but the answer apparently is to wrap it in an external module declaration:

declare module 'my-config' {
  global {
    interface Window {
      CONFIG: any
    }
  }
}

Note that you do not need to import my-config anywhere, and the name of that take module seems irrelevant. window.CONFIG will simply be available globally.

like image 167
miracle2k Avatar answered Sep 03 '26 10:09

miracle2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!