Within my schema
import { DocumentReference } from 'angularfire2/firestore';
type DogType = 'good' | 'bad';
interface Dog {
ref: DocumentReference;
}
interface GoodDog extends Dog {
props: GoodDogProps;
}
interface GoodDogProps {
// Props
}
interface BadDog extends Dog {
props: BadDogProps;
}
interface BadDogProps {
// Props
}
I also have an ApiService
@Injectable({
providedIn: 'root'
})
export class DogApiService {
private dogs: {
good: AngularFirestoreCollection<GoodDogProps>;
bad: AngularFirestoreCollection<GoodDogProps>;
};
constructor(private db: AngularFirestore) {
this.dogs = {
good: this.db.collection('goodDogs'),
bad: this.db.collection('badDogs')
};
}
public listDogs(type: DogType) {
return this.dogs[type].ref.get().then(collection =>
collection.docs.map(doc => {
return {
ref: doc.ref,
props: doc.data()
};
})
);
}
}
But I am facing an issue with TypeScript:
When I call listDogs('good')
, I want TypeScript to indicate that this function will return a Promise<GoodDogs[]>
. I attempted using generics but couldn't resolve the TS errors.
A bit of context:
- I cannot utilize
AngularFirestore.valueChanges()
as it doesn't provideDocumentReference
- I prefer not to use the alternative
AngularFirestore.snapshotChanges
method, as I solely require a simple Promise GoodDog
andBadDog
are the types I need for my AppGoodDogProps
andBadDogProps
define the Firebase schema