'Define global firebase constant in Typescript
I have a service worker written in Typescript that does this:
export default null;
declare const self: ServiceWorkerGlobalScope;
declare const firebase: any // ### FIX THIS. This needs to be typed as firebase ####
// the imports below expose firebase global object
self.importScripts('https://www.gstatic.com/firebasejs/8.9.1/firebase-app.js');
self.importScripts(
'https://www.gstatic.com/firebasejs/8.9.1/firebase-messaging.js'
);
self.addEventListener('install', function () {
// do stuff here.
firebase.initializeApp();
});
It's defining firebase as a const of type any. I want it to be the same type one gets by doing
import firebase from 'firebase/app';
I would like to not import/bundle firebase in my service worker, because that significantly bloats my service worker. The import scripts already expose everything I need (i.e., they expose a global firebase for me).
I was doing this before, but webpack makes the file from 800 bytes to 80kb.
import firebase from 'firebase/app';
How can I get the types of firebase without importing all the code that comes with firebase/app?
I've tried this:
export default null;
declare const self: ServiceWorkerGlobalScope;
/// <reference types="firebase/app" />
declare const firebase: firebase;
But I get the error Cannot use namaespace 'firebase' as a type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
