i am trying to access the globalThis property, but get the error:
Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature`.
// both getting...
if (!globalThis.foo) {}
// and setting...
globalThis.foo = 'bar
the only thing i can find online about this, refers to using window, and provides declarations to support it, but not for globalThis. anyone know how to support this?
According to the globalThis documentation, it looks like the "right" way to do this is to declare a global var named foo. That will add to globalThis.
If your code is in global scope, then this will work:
var foo: string;
If your code is in a module, you need to wrap that in a global declaration:
export const thisIsAModule = true; // <-- definitely in a module
declare global {
var foo: string;
}
After that, you should be able to access globalThis.foo as desired:
globalThis.foo = "bar"; // no error
Playground link to code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With