React-native-firebase: Typescript not working on `firebase.` unless used as `firebase.app().`

Created on 23 Jan 2018  路  13Comments  路  Source: invertase/react-native-firebase

Issue

I was using firebase.app().auth previously until discovering that app() bit is no longer needed. Switching to firebase.auth causes typescript error below (not only for auth, but in general)

example

[ts] Property 'auth' does not exist on type 'typeof FireBase'.
any

Environment

  1. Application Target Platform: iOS, Android

  1. Development Operating System: macOS High Sierra

  1. Build Tools:

  1. React Native version: 0.52

  1. RNFirebase Version: 3.2.1

  1. Firebase Module:
Typings

Most helpful comment

@Ehesp No problem, I'll submit a PR once I've got something ready

All 13 comments

@IljaDaderko does this happen on the other modules as well?

@Ehesp indeed it does, judging by typescript suggestions a lot is missing, see screenshots for both firebase. and firebase.app(). below (everything seems to be typed correctly for app())

firebase

firebase.app()

@pheromonez do you mind taking a look at this issue?

@Ehesp No problem, I'll submit a PR once I've got something ready

Awesome thanks very much!

What works for me is to first instantiate a firebase object (for example in an injection container like TSDI) const firebase = new FireBase().
And then use the modules on that instance like firebase.auth(), firebase.links(), etc.
I always assumed that was intended behavior 馃槄

@danielw93 That did the trick for me, but docs show implementation that is more concise with web sdk: https://rnfirebase.io/docs/v3.2.x/installation/javascript in my opinion this is more friendly to web devs coming into react-native-firebase.

firestore also seems to be missing from type defs. I tried the new Firebase approach that @danielw93 suggested but got errors saying that Firebase is not a constructor. I鈥檝e resorted to commonjs imports to skip type checking, which is definitely less than ideal:

import firebase from 'react-native-firebase';
const firebase2 = require('react-native-firebase');

// ...

firebase.app().auth().signInAnonymously().then(user => {
  const docRef = firebase2.default.firestore().collection('users').doc(user.uid);
  // ...
});

@pheromonez let me know if there鈥檚 anything I can do to help with fixing type definitions.

I dug around a bit to see if I could find a flow to .d.ts generator, but didn鈥檛 have a lot of luck. Seems like this should be an automated thing.

@meyer there's currently a PR for Firestore

@Ehesp I've submitted a PR to improve the TS definitions. While going through the JS code I noticed that there's a utils package which is undocumented on https://rnfirebase.io/. Is this package intended for internal use only?

@meyer Thanks for the offer to help. This fix was fairly small, so I've been able to complete it, but there are a couple of modules that currently don't have any TS definitions (admob, config, perf) and could certainly use some attention.

@pheromonez yeah the utils directory is used for things like internal event emitters, common helpers and the like and isn't designed for public consumption.

This may not be the right place, but I'm wondering what the rationale for returning multiple values was?
For example, calling firestore.data() will return the following:

Screen Shot 2019-03-20 at 12 42 30 PM

For Javascript it doesn't seem to matter, but for Typescript I've had to structure my code differently in comparison to if I was just using that standard Firebase.firestore SDK.

Was this page helpful?
0 / 5 - 0 ratings