如何让Typescript知道您正在HTML中的<scripts>之间共享数据?

问题描述

我专门使用Firebase,但这是一个普遍问题,适用于任何多脚本用例。

基本上,我在一个脚本中声明const firebaseConfig = { ... };,然后在要进行捆绑过程的单独文件中的React代码中引用该变量。

当然,VS Code会说“嘿,firebaseConfig不存在!错误!错误!”但实际上确实如此。

如何解决这个问题,以免在开发过程中出现误导性错误?

解决方法

对于打字稿,只需使用:

declare global {
   const firebaseConfig: any; // or give it a proper type,up to you
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...