firebase / geofire-js

GeoFire for JavaScript - Realtime location queries with Firebase
MIT License
1.44k stars 345 forks source link

Can't instantiate Geofire ref in TS #200

Closed adriansergheev closed 3 years ago

adriansergheev commented 3 years ago

This piece of code:

    var database = admin.database().ref()
        .child('geoData');
    var geoFireDB = new GeoFire.GeoFire(database)

Does not work. Error:

Argument of type 'import("").admin.database.Reference' is not assignable to parameter of type 'import("").Reference'. The types of 'push(...).endAt(...).isEqual' are incompatible between these types.

Type '(other: import("functions/node_modules/firebase-admin/lib/database").admin.database.Query | null) => boolean' is not assignable to type '(other: import("/cloudFunctions/functions/node_modules/@firebase/database-types/index").Query | null) => boolean'. Types of parameters 'other' and 'other' are incompatible. etc.

Here is what I have defined in package.json: "firebase": "^7.24.0", "firebase-admin": "^8.12.1", "firebase-functions": "^3.11.0", "geofire": "^5.0.1",

Any ideas?

ziyaddin commented 3 years ago

Could you also show your import statement?

adriansergheev commented 3 years ago

import * as GeoFire from 'geofire';

Thanks!

ziyaddin commented 3 years ago

Oh, I remembered. It happened to me too. The reason of error is that when GeoFire is being initialized, it requires Reference typed object without the path property but firebase-admin creates Reference typed object with the path property. Use the code below to fix the typing error:

var geoFireDB = new GeoFire.GeoFire(database as unknown as GeoFire.GeoFireTypes.firebase.Reference);

So, basically what id does: first, it transforms firebase-admin's Reference typed object's type to unknown and then to GeoFire's Reference type.

ziyaddin commented 3 years ago

@andriansergheev any success?

adriansergheev commented 3 years ago

Works. I'm coming from a strong-typed world and this kind of dances are incomprehensible for my brain, Thanks again,

ziyaddin commented 3 years ago

You are welcome.