Skip to content

Commit ca2a243

Browse files
committed
Trying to fix firestore-types
1 parent b065c0d commit ca2a243

File tree

1 file changed

+57
-55
lines changed

1 file changed

+57
-55
lines changed

packages/firestore-types/index.d.ts

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ export type LogLevel =
4949

5050
export function setLogLevel(logLevel: LogLevel): void;
5151

52-
export interface FirestoreDataConverter<T> {
53-
toFirestore(modelObject: T): DocumentData;
54-
toFirestore(modelObject: Partial<T>, options: SetOptions): DocumentData;
55-
56-
fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): T;
52+
export interface FirestoreDataConverter<
53+
AppModelType,
54+
DbModelType extends DocumentData = DocumentData
55+
> {
56+
toFirestore(modelObject: AppModelType): DocumentData;
57+
toFirestore(modelObject: Partial<AppModelType>, options: SetOptions): DocumentData;
58+
59+
fromFirestore(snapshot: QueryDocumentSnapshot<AppModelType, DbModelType>, options: SnapshotOptions): AppModelType;
5760
}
5861

5962
export class FirebaseFirestore {
@@ -73,7 +76,7 @@ export class FirebaseFirestore {
7376

7477
collection(collectionPath: string): CollectionReference<DocumentData>;
7578

76-
doc(documentPath: string): DocumentReference<DocumentData>;
79+
doc(documentPath: string): DocumentReference<DocumentData, DocumentData>;
7780

7881
collectionGroup(collectionId: string): Query<DocumentData>;
7982

@@ -185,14 +188,14 @@ export class Blob {
185188
export class Transaction {
186189
private constructor();
187190

188-
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
191+
get<T>(documentRef: DocumentReference<AppModelType, DbModelType>): Promise<DocumentSnapshot<AppModelType, DbModelType>>;
189192

190193
set<T>(
191-
documentRef: DocumentReference<T>,
194+
documentRef: DocumentReference<AppModelType, DbModelType>,
192195
data: Partial<T>,
193196
options: SetOptions
194197
): Transaction;
195-
set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
198+
set<T>(documentRef: DocumentReference<AppModelType, DbModelType>, data: T): Transaction;
196199

197200
update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
198201
update(
@@ -209,11 +212,11 @@ export class WriteBatch {
209212
private constructor();
210213

211214
set<T>(
212-
documentRef: DocumentReference<T>,
215+
documentRef: DocumentReference<AppModelType, DbModelType>,
213216
data: Partial<T>,
214217
options: SetOptions
215218
): WriteBatch;
216-
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
219+
set<T>(documentRef: DocumentReference<AppModelType, DbModelType>, data: T): WriteBatch;
217220

218221
update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
219222
update(
@@ -241,17 +244,17 @@ export interface GetOptions {
241244
readonly source?: 'default' | 'server' | 'cache';
242245
}
243246

244-
export class DocumentReference<T = DocumentData> {
247+
export class DocumentReference<AppModelType, DbModelType> {
245248
private constructor();
246249

247250
readonly id: string;
248251
readonly firestore: FirebaseFirestore;
249-
readonly parent: CollectionReference<T>;
252+
readonly parent: CollectionReference<AppModelType, DbModelType>;
250253
readonly path: string;
251254

252255
collection(collectionPath: string): CollectionReference<DocumentData>;
253256

254-
isEqual(other: DocumentReference<T>): boolean;
257+
isEqual(other: DocumentReference<AppModelType, DbModelType>): boolean;
255258

256259
set(data: Partial<T>, options: SetOptions): Promise<void>;
257260
set(data: T): Promise<void>;
@@ -265,29 +268,29 @@ export class DocumentReference<T = DocumentData> {
265268

266269
delete(): Promise<void>;
267270

268-
get(options?: GetOptions): Promise<DocumentSnapshot<T>>;
271+
get(options?: GetOptions): Promise<DocumentSnapshot<AppModelType, DbModelType>>;
269272

270273
onSnapshot(observer: {
271-
next?: (snapshot: DocumentSnapshot<T>) => void;
274+
next?: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void;
272275
error?: (error: FirestoreError) => void;
273276
complete?: () => void;
274277
}): () => void;
275278
onSnapshot(
276279
options: SnapshotListenOptions,
277280
observer: {
278-
next?: (snapshot: DocumentSnapshot<T>) => void;
281+
next?: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void;
279282
error?: (error: FirestoreError) => void;
280283
complete?: () => void;
281284
}
282285
): () => void;
283286
onSnapshot(
284-
onNext: (snapshot: DocumentSnapshot<T>) => void,
287+
onNext: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void,
285288
onError?: (error: FirestoreError) => void,
286289
onCompletion?: () => void
287290
): () => void;
288291
onSnapshot(
289292
options: SnapshotListenOptions,
290-
onNext: (snapshot: DocumentSnapshot<T>) => void,
293+
onNext: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void,
291294
onError?: (error: FirestoreError) => void,
292295
onCompletion?: () => void
293296
): () => void;
@@ -307,24 +310,23 @@ export interface SnapshotMetadata {
307310
isEqual(other: SnapshotMetadata): boolean;
308311
}
309312

310-
export class DocumentSnapshot<T = DocumentData> {
313+
export class DocumentSnapshot<AppModelType, DbModelType> {
311314
protected constructor();
312315

313316
readonly exists: boolean;
314-
readonly ref: DocumentReference<T>;
317+
readonly ref: DocumentReference<AppModelType, DbModelType>;
315318
readonly id: string;
316319
readonly metadata: SnapshotMetadata;
317320

318321
data(options?: SnapshotOptions): T | undefined;
319322

320323
get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
321324

322-
isEqual(other: DocumentSnapshot<T>): boolean;
325+
isEqual(other: DocumentSnapshot<AppModelType, DbModelType>): boolean;
323326
}
324327

325-
export class QueryDocumentSnapshot<
326-
T = DocumentData
327-
> extends DocumentSnapshot<T> {
328+
export class QueryDocumentSnapshot<AppModelType, DbModelType>
329+
extends DocumentSnapshot<AppModelType, DbModelType> {
328330
private constructor();
329331

330332
data(options?: SnapshotOptions): T;
@@ -344,7 +346,7 @@ export type WhereFilterOp =
344346
| 'array-contains-any'
345347
| 'not-in';
346348

347-
export class Query<T = DocumentData> {
349+
export class Query<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>{
348350
protected constructor();
349351

350352
readonly firestore: FirebaseFirestore;
@@ -353,54 +355,54 @@ export class Query<T = DocumentData> {
353355
fieldPath: string | FieldPath,
354356
opStr: WhereFilterOp,
355357
value: any
356-
): Query<T>;
358+
): Query<AppModelType, DbModelType>;
357359

358360
orderBy(
359361
fieldPath: string | FieldPath,
360362
directionStr?: OrderByDirection
361-
): Query<T>;
363+
): Query<AppModelType, DbModelType>;
362364

363-
limit(limit: number): Query<T>;
365+
limit(limit: number): Query<AppModelType, DbModelType>;
364366

365-
limitToLast(limit: number): Query<T>;
367+
limitToLast(limit: number): Query<AppModelType, DbModelType>;
366368

367-
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
368-
startAt(...fieldValues: any[]): Query<T>;
369+
startAt(snapshot: DocumentSnapshot<AppModelType, DbModelType>): Query<AppModelType, DbModelType>;
370+
startAt(...fieldValues: any[]): Query<AppModelType, DbModelType>;
369371

370-
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
371-
startAfter(...fieldValues: any[]): Query<T>;
372+
startAfter(snapshot: DocumentSnapshot<AppModelType, DbModelType>): Query<AppModelType, DbModelType>;
373+
startAfter(...fieldValues: any[]): Query<AppModelType, DbModelType>;
372374

373-
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
374-
endBefore(...fieldValues: any[]): Query<T>;
375+
endBefore(snapshot: DocumentSnapshot<AppModelType, DbModelType>): Query<AppModelType, DbModelType>;
376+
endBefore(...fieldValues: any[]): Query<AppModelType, DbModelType>;
375377

376-
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
377-
endAt(...fieldValues: any[]): Query<T>;
378+
endAt(snapshot: DocumentSnapshot<AppModelType, DbModelType>): Query<AppModelType, DbModelType>;
379+
endAt(...fieldValues: any[]): Query<AppModelType, DbModelType>;
378380

379-
isEqual(other: Query<T>): boolean;
381+
isEqual(other: Query<AppModelType, DbModelType>): boolean;
380382

381-
get(options?: GetOptions): Promise<QuerySnapshot<T>>;
383+
get(options?: GetOptions): Promise<QuerySnapshot<AppModelType, DbModelType>>;
382384

383385
onSnapshot(observer: {
384-
next?: (snapshot: QuerySnapshot<T>) => void;
386+
next?: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void;
385387
error?: (error: FirestoreError) => void;
386388
complete?: () => void;
387389
}): () => void;
388390
onSnapshot(
389391
options: SnapshotListenOptions,
390392
observer: {
391-
next?: (snapshot: QuerySnapshot<T>) => void;
393+
next?: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void;
392394
error?: (error: FirestoreError) => void;
393395
complete?: () => void;
394396
}
395397
): () => void;
396398
onSnapshot(
397-
onNext: (snapshot: QuerySnapshot<T>) => void,
399+
onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void,
398400
onError?: (error: FirestoreError) => void,
399401
onCompletion?: () => void
400402
): () => void;
401403
onSnapshot(
402404
options: SnapshotListenOptions,
403-
onNext: (snapshot: QuerySnapshot<T>) => void,
405+
onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void,
404406
onError?: (error: FirestoreError) => void,
405407
onCompletion?: () => void
406408
): () => void;
@@ -409,44 +411,44 @@ export class Query<T = DocumentData> {
409411
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
410412
}
411413

412-
export class QuerySnapshot<T = DocumentData> {
414+
export class QuerySnapshot<AppModelType, DbModelType> {
413415
private constructor();
414416

415-
readonly query: Query<T>;
417+
readonly query: Query<AppModelType, DbModelType>;
416418
readonly metadata: SnapshotMetadata;
417-
readonly docs: Array<QueryDocumentSnapshot<T>>;
419+
readonly docs: Array<QueryDocumentSnapshot<AppModelType, DbModelType>>;
418420
readonly size: number;
419421
readonly empty: boolean;
420422

421423
docChanges(options?: SnapshotListenOptions): Array<DocumentChange<T>>;
422424

423425
forEach(
424-
callback: (result: QueryDocumentSnapshot<T>) => void,
426+
callback: (result: QueryDocumentSnapshot<AppModelType, DbModelType>) => void,
425427
thisArg?: any
426428
): void;
427429

428-
isEqual(other: QuerySnapshot<T>): boolean;
430+
isEqual(other: QuerySnapshot<AppModelType, DbModelType>): boolean;
429431
}
430432

431433
export type DocumentChangeType = 'added' | 'removed' | 'modified';
432434

433-
export interface DocumentChange<T = DocumentData> {
435+
export interface DocumentChange<AppModelType, DbModelType> {
434436
readonly type: DocumentChangeType;
435-
readonly doc: QueryDocumentSnapshot<T>;
437+
readonly doc: QueryDocumentSnapshot<AppModelType, DbModelType>;
436438
readonly oldIndex: number;
437439
readonly newIndex: number;
438440
}
439441

440-
export class CollectionReference<T = DocumentData> extends Query<T> {
442+
export class CollectionReference<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> extends Query<AppModelType, DbModelType> {
441443
private constructor();
442444

443445
readonly id: string;
444-
readonly parent: DocumentReference<DocumentData> | null;
446+
readonly parent: DocumentReference<AppModelType, DbModelType>| null;
445447
readonly path: string;
446448

447-
doc(documentPath?: string): DocumentReference<T>;
449+
doc(documentPath?: string): DocumentReference<AppModelType, DbModelType>;
448450

449-
add(data: T): Promise<DocumentReference<T>>;
451+
add(data: T): Promise<DocumentReference<AppModelType, DbModelType>>;
450452

451453
isEqual(other: CollectionReference<T>): boolean;
452454

0 commit comments

Comments
 (0)