I\'m trying to set up GeoFire in my Angular2 (RC5) app. I\'ve installed geofire and firebase with npm and configured systemjs to import it. Here\'s my package.json:
Notice, I'm still not a TypeScript expert and this is my first time dabbling with type definitions and such, but after a long work day I seem to have got something that works (at least for my setup). I'm using Ionic 2 RC2, Angular2 2.1.1, Geofire 4.1.2. The build uses webpack. I created geofire.d.ts in my app directory with the following:
declare module 'geofire' {
type EventType = 'ready' | 'key_entered' | 'key_exited' | 'key_moved';
interface GeoQueryCriteria {
center: number[];
radius: number;
}
interface GeoQueryUpdateCriteria {
center?: number[];
radius?: number;
}
interface GeoCallbackRegistration {
cancel();
}
interface GeoQuery {
center(): number[];
radius(): number;
updateCriteria(criteria: GeoQueryUpdateCriteria);
on(eventType: EventType, callback: (key:string, location: number[], distance: number) => void): GeoCallbackRegistration;
cancel();
}
class GeoFire {
constructor(ref: any);
ref(): any;
set(key: string, loc: number[]): Promise;
get(key: string): Promise;
remove(key: string): Promise;
query(criteria: GeoQueryCriteria): GeoQuery;
static distance(location1: number[], location2: number[]);
}
export = GeoFire;
}
Finally in my Service/Page I can import GeoFire like so:
import GeoFire from 'geofire';
Note the exact syntax of the import there are no curly braces {}. I tried so many ways to get it to work differently but I think the way the javascript was written, this is how it has to be done. I found ways that would get rid of compile errors, but fail at runtime ('no constructor named...'), or it would recognize the constructor in compilation but not as a type.
I only created it for the public API so if you try to extend GeoFire for some reason be aware there could be name conflicts with other methods not exposed in this definition file.
If this helps you out please let me know and I'll contribute it to DefinitelyTyped so no one else has to go through the pain I (and obviously others with 340+ views) did.