1

I have this basic codebase

import { FirebaseApp, FirebaseOptions } from "firebase/app";

function example(app){
// error: FirebaseApp only refers to a type
 console.log(app instanceof FirebaseApp)
}

here FirebaseApp is a typescript interface which is not exists in runtime, how to check if the provided arg is an instance of firebase app

Sh eldeeb
  • 1,589
  • 3
  • 18
  • 41

1 Answers1

0

FirebaseApp is just an interface and not a class. The app is an instance of class FirebaseAppImpl (implementation of FirebaseApp) and cannot be imported from firebase/app. If you are just trying to pass the Firebase App instance around, then use getApp() function instead:

import { getApp } from "firebase/app";

const firebaseApp = getApp(); // default Firebase app instance
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • 1
    `app === getApp()` does return true if there are the _same_ app instance. But if this is major requirement, Firebase SDKs are open source (linked in answer) and you should be able to expose `FirebaseAppImpl` for your use case. – Dharmaraj Nov 09 '22 at 18:38
  • I don't want to get the default app, I just want to check that the provided `app` is an instance of firebase app – Sh eldeeb Nov 09 '22 at 19:20
  • @Sheldeeb I would recommend checking out the source code and exposing the `FirebaseAppImpl` class then and perhaps send a raise an feature request issue in Github. Take a look at [Check if object implements interface](https://stackoverflow.com/questions/33800497/check-if-an-object-implements-an-interface-at-runtime-with-typescript) also. – Dharmaraj Nov 09 '22 at 19:21
  • FirebaseAppImpl, UserCredentialsImpl and other *Impl classes are not exported by firebase – Sh eldeeb Nov 09 '22 at 20:01
  • 1
    @Sheldeeb that's what I mentioned in my answer. You might have to modify the existing source code a bit if you want to use them. – Dharmaraj Nov 09 '22 at 20:06