- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试访问 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。有人知道如何支持吗?
最佳答案
根据the globalThis
documentation ,看起来“正确”的方法是声明一个全局 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
Playground link to code
关于javascript - 访问 globalThis 属性的 typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64195920/
我在 React WebApp 中使用 globalThis 属性,特别是 globalThis.scrollTo(0,0)。 我使用 Jest 和 Enzyme 进行单元测试。 截至测试用例失败,因
问题 我正在使用 mysql 构建一个 TypeScript Express 应用程序。初始化数据库连接池 const pool = mysql.createPool(...) 后,我想将创建的 Po
我有一个基于 angular 9 的网站,托管在 firebase 中。我有一个问题只发生在某些设备上,如 iphone 6/7/8。不是全部,但确实经常。 错误文本:main-es5.d57a465
我正在尝试在 Typescript 中使用 globalThis,我想就如何以更好的方式编写它提出一些建议。 目前的实现是这样的: 创建一个文件 types/global.d.ts 并在里面添加 in
我正在尝试访问 globalThis属性,但得到错误: Element implicitly has an 'any' type because type 'typeof globalThis' ha
VSCode 自动补全最近开始在 JavaScript 文件中将“this”转换为“globalThis”。 这非常烦人,每次我想写“this.something”时,我都需要按 ctrl+z 来摆脱
最近我遇到了globalThis在 Javascript 中。我不确定如果从函数调用它会如何表现。每次返回 window目的。如果是这样,那我们为什么不直接使用 window目的。使用 globalT
let globalThis = undefined在浏览器中没有给我任何错误let [global, globalThis] = [undefined, undefined]在 Node 中没有给我
在 Electron-React-Typescript 应用程序中,我收到此错误:Property 'api' does not exist on type 'Window & typeof glob
我有下面的代码,自动生成: $ react-native init RepeatAloud 应用程序.tsx /** * Sample React Native App * https://git
我正在尝试来自 github 的 angular 项目。我正在使用 node 版本 12、angular cli 9 和更新的 package.json。 https://github.com/cha
我在我的 Vue/Firebase 应用程序上设置了 App Check。 为了使用 App Check 启用本地开发,我正在关注 intructions here . 这些说明要求您像这样设置调试
我在我的 Vue/Firebase 应用程序上设置了 App Check。 为了使用 App Check 启用本地开发,我正在关注 intructions here . 这些说明要求您像这样设置调试
尝试运行 @angular/fire在 Angular 11 和 @nguniversal/express-engine 上(SSR)。 初始化时AngularFireModule在 app.modu
编辑:这是一个 ESlint 问题。我在本文末尾添加了我的 ESLint 设置。 我对 TypeScript 相当陌生——当我遇到这一行时,我一直在重构别人的 React 代码,将其移动到 TypeS
我正在使用 angular 7.2.6、angular-cli 7.3.9 和 typescript 3.2.4。构建 Angular 应用程序时出现错误 node_modules/@types/no
我是一名优秀的程序员,十分优秀!