Skip to content

Commit 730252f

Browse files
committed
Clean up the types some more
1 parent c46149a commit 730252f

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/firestore/collection/collection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DocumentChangeType, CollectionReference, Query, DocumentReference } from '@firebase/firestore-types';
1+
import { DocumentChangeType, CollectionReference, Query, DocumentReference, DocumentData } from '@firebase/firestore-types';
22
import { Observable, Subscriber } from 'rxjs';
33
import { fromCollectionRef } from '../observable/fromRef';
44
import { map, filter } from 'rxjs/operators';
@@ -40,7 +40,7 @@ export function validateEventsArray(events?: DocumentChangeType[]) {
4040
* // Subscribe to changes as snapshots. This provides you data updates as well as delta updates.
4141
* fakeStock.valueChanges().subscribe(value => console.log(value));
4242
*/
43-
export class AngularFirestoreCollection<T> {
43+
export class AngularFirestoreCollection<T=DocumentData> {
4444
/**
4545
* The constructor takes in a CollectionReference and Query to provide wrapper methods
4646
* for data operations and data streaming.
@@ -106,11 +106,11 @@ export class AngularFirestoreCollection<T> {
106106
* Listen to all documents in the collection and its possible query as an Observable.
107107
*/
108108
valueChanges(): Observable<T[]> {
109-
const fromCollectionRef$ = fromCollectionRef(this.query);
109+
const fromCollectionRef$ = fromCollectionRef<T>(this.query);
110110
const scheduled$ = this.afs.scheduler.runOutsideAngular(fromCollectionRef$);
111111
return this.afs.scheduler.keepUnstableUntilFirst(scheduled$)
112112
.pipe(
113-
map(actions => actions.payload.docs.map(a => a.data()) as T[])
113+
map(actions => actions.payload.docs.map(a => a.data()))
114114
);
115115
}
116116

src/firestore/document/document.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DocumentReference, SetOptions } from '@firebase/firestore-types';
1+
import { DocumentReference, SetOptions, DocumentData } from '@firebase/firestore-types';
22
import { Observable, Subscriber } from 'rxjs';
33
import { QueryFn, AssociatedReference, Action, DocumentSnapshot } from '../interfaces';
44
import { fromDocRef } from '../observable/fromRef';
@@ -31,7 +31,7 @@ import { AngularFirestoreCollection } from '../collection/collection';
3131
* // OR! Transform using Observable.from() and the data is unwrapped for you
3232
* Observable.from(fakeStock).subscribe(value => console.log(value));
3333
*/
34-
export class AngularFirestoreDocument<T> {
34+
export class AngularFirestoreDocument<T=DocumentData> {
3535

3636
/**
3737
* The contstuctor takes in a DocumentReference to provide wrapper methods
@@ -88,10 +88,10 @@ export class AngularFirestoreDocument<T> {
8888
/**
8989
* Listen to unwrapped snapshot updates from the document.
9090
*/
91-
valueChanges(): Observable<T|null> {
91+
valueChanges(): Observable<T|undefined> {
9292
return this.snapshotChanges().pipe(
9393
map(action => {
94-
return action.payload.exists ? action.payload.data() as T : null;
94+
return action.payload.data();
9595
})
9696
);
9797
}

src/firestore/interfaces.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Subscriber } from 'rxjs';
2-
import { DocumentChangeType, FieldPath, DocumentSnapshot as _DocumentSnapshot, SnapshotOptions, QueryDocumentSnapshot as _QueryDocumentSnapshot, DocumentChange as _DocumentChange, CollectionReference, Query } from '@firebase/firestore-types';
2+
import { DocumentChangeType, QuerySnapshot as _QuerySnapshot, FieldPath, DocumentSnapshot as _DocumentSnapshot, SnapshotOptions, QueryDocumentSnapshot as _QueryDocumentSnapshot, DocumentChange as _DocumentChange, CollectionReference, Query } from '@firebase/firestore-types';
33

44
export interface DocumentSnapshotExists<T> extends _DocumentSnapshot {
55
readonly exists: true;
@@ -18,6 +18,10 @@ export interface QueryDocumentSnapshot<T> extends _QueryDocumentSnapshot {
1818
data(options?: SnapshotOptions): T;
1919
}
2020

21+
export interface QuerySnapshot<T> extends _QuerySnapshot {
22+
readonly docs: QueryDocumentSnapshot<T>[];
23+
}
24+
2125
export interface DocumentChange<T> extends _DocumentChange {
2226
readonly doc: QueryDocumentSnapshot<T>;
2327
}

src/firestore/observable/fromRef.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { DocumentReference, Query, QuerySnapshot } from '@firebase/firestore-types';
1+
import { DocumentReference, Query } from '@firebase/firestore-types';
22
import { Observable, Subscriber } from 'rxjs';
3-
import { Action, Reference, DocumentSnapshot } from '../interfaces';
3+
import { Action, Reference, DocumentSnapshot, QuerySnapshot } from '../interfaces';
44
import { map, share } from 'rxjs/operators';
55

66
function _fromRef<T, R>(ref: Reference<T>): Observable<R> {
@@ -21,6 +21,6 @@ export function fromDocRef<T>(ref: DocumentReference): Observable<Action<Documen
2121
);
2222
}
2323

24-
export function fromCollectionRef(ref: Query): Observable<Action<QuerySnapshot>> {
25-
return fromRef<QuerySnapshot>(ref).pipe(map(payload => ({ payload, type: 'query' })));
24+
export function fromCollectionRef<T>(ref: Query): Observable<Action<QuerySnapshot<T>>> {
25+
return fromRef<QuerySnapshot<T>>(ref).pipe(map(payload => ({ payload, type: 'query' })));
2626
}

0 commit comments

Comments
 (0)