我正在尝试访问该globalThis
属性,但收到错误:
Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature`.
// both getting...
if (!globalThis.foo) {}
// and setting...
globalThis.foo = 'bar
我在网上可以找到的唯一内容是使用window
,并提供了支持它的声明,但没有针对globalThis。有人知道如何支持吗?
根据该globalThis
文件,它看起来像“正确”的方式做到这一点是要声明一个全局var
命名foo
。这将增加globalThis
。
如果您的代码在全局范围内,那么它将起作用:
var foo: string;
如果您的代码在模块中,则需要将其包装在global
声明中:
export const thisIsAModule = true; // <-- definitely in a module
declare global {
var foo: string;
}
之后,您应该可以globalThis.foo
根据需要访问:
globalThis.foo = "bar"; // no error
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句