Skip to content

Instantly share code, notes, and snippets.

@esnya
Last active October 15, 2019 09:02
Show Gist options
  • Save esnya/ec751ec514f78505af4dde406eb6b32c to your computer and use it in GitHub Desktop.
Save esnya/ec751ec514f78505af4dde406eb6b32c to your computer and use it in GitHub Desktop.
firestore-typed
import { firestore } from 'firebase/app';
export type FieldPath = firestore.FieldPath;
export type FirestoreError = firestore.FirestoreError;
export type GetOptions = firestore.GetOptions;
export type OrderByDirection = firestore.OrderByDirection;
export type SetOptions = firestore.SetOptions;
export type SnapshotListenOptions = firestore.SnapshotListenOptions;
export type SnapshotOptions = firestore.SnapshotOptions;
export interface Observer<T, E = Error> {
complete?: () => void;
error?: (error: E) => void;
next?: (snapshot: T) => void;
}
export interface Firestore extends firestore.Firestore {
collection<T>(collectionPath: string): CollectionReference<T>;
}
export interface Query<T> extends firestore.Query {
endAt(snapshot: DocumentSnapshot<T>): Query<T>;
endAt(...fieldValues: any[]): Query<T>;
endBefore(snapshot: DocumentSnapshot<T>): Query<T>;
endBefore(...fieldValues: any[]): Query<T>;
get(options?: GetOptions): Promise<QuerySnapshot<T>>;
limit(limit: number): Query<T>;
onSnapshot(observer: QueryObserver<T>): () => {};
onSnapshot(
options: SnapshotListenOptions,
observer: QueryObserver<T>,
): () => {};
onSnapshot(
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
orderBy(
fieldPath: string | FieldPath,
directionStr?: OrderByDirection,
): Query<T>;
startAfter(snapshot: DocumentSnapshot<T>): Query<T>;
startAfter(...fieldValues: any[]): Query<T>;
startAt(snapshot: DocumentSnapshot<T>): Query<T>;
startAt(...fieldValues: any[]): Query<T>;
}
export type QueryObserver<T> = Observer<QuerySnapshot<T>>;
export interface QuerySnapshot<T> extends firestore.QuerySnapshot {
docs: QueryDocumentSnapshot<T>[];
query: Query<T>;
docChanges(options?: SnapshotListenOptions): DocumentChange<T>[];
forEach(
callback: (result: QueryDocumentSnapshot<T>) => void,
thisArg?: any,
): void;
}
export interface QueryDocumentSnapshot<T>
extends Omit<firestore.QueryDocumentSnapshot, keyof DocumentSnapshot<T>>,
DocumentSnapshot<T> {
data(options?: SnapshotOptions): DocumentData<T>;
}
export interface DocumentChange<T> extends firestore.DocumentChange {
doc: QueryDocumentSnapshot<T>;
}
export interface CollectionReference<T, U = never>
extends Omit<firestore.CollectionReference, keyof Query<T>>,
Query<T> {
parent: DocumentReference<U> | null;
add(data: T): Promise<DocumentReference<T>>;
doc<U = T>(documentPath?: string): DocumentReference<U>;
}
export interface DocumentReference<T> extends firestore.DocumentReference {
parent: CollectionReference<T>;
collection<U>(collectionPath: string): CollectionReference<U>;
get(options?: GetOptions): Promise<DocumentSnapshot<T>>;
onSnapshot(observer: DocumentObserver<T>): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: DocumentObserver<T>,
): () => void;
onSnapshot(
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
set(data: T, options?: SetOptions): Promise<void>;
update(data: Partial<T>): Promise<void>;
update(
field: string | FieldPath,
value: any,
...moreFieldsAndValues: any[]
): Promise<void>;
}
export type DocumentObserver<T> = Observer<DocumentSnapshot<T>, FirestoreError>;
export interface DocumentSnapshot<T> extends firestore.DocumentSnapshot {
ref: DocumentReference<T>;
data(options?: SnapshotOptions): DocumentData<T> | undefined;
get<U = any>(
fieldPath: string | FieldPath,
options?: SnapshotOptions,
): U | undefined;
}
export type DocumentData<T> = Partial<T>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment