diff --git a/packages/firestore/lib/index.d.ts b/packages/firestore/lib/index.d.ts index bf7c5e3233..8ea648be9b 100644 --- a/packages/firestore/lib/index.d.ts +++ b/packages/firestore/lib/index.d.ts @@ -86,11 +86,18 @@ export namespace FirebaseFirestoreTypes { toUint8Array(): Uint8Array; } + /** + * A `DocumentData` object represents the data in a document. + */ + export interface DocumentData { + [key: string]: any; + } + /** * A `CollectionReference` object can be used for adding documents, getting document references, and querying for * documents (using the methods inherited from `Query`). */ - export interface CollectionReference extends Query { + export interface CollectionReference extends Query { /** * The collection's identifier. */ @@ -121,7 +128,7 @@ export namespace FirebaseFirestoreTypes { * * @param data An Object containing the data for the new document. */ - add(data: { [key: string]: value }): Promise; + add(data: T): Promise>; /** * Get a DocumentReference for the document within the collection at the specified path. If no @@ -138,18 +145,18 @@ export namespace FirebaseFirestoreTypes { * * @param documentPath A slash-separated path to a document. */ - doc(documentPath?: string): DocumentReference; + doc(documentPath?: string): DocumentReference; } /** * A DocumentChange represents a change to the documents matching a query. It contains the document affected and the * type of change that occurred. */ - export interface DocumentChange { + export interface DocumentChange { /** * The document affected by this change. */ - doc: QueryDocumentSnapshot; + doc: QueryDocumentSnapshot; /** * The index of the changed document in the result set immediately after this `DocumentChange` @@ -198,7 +205,7 @@ export namespace FirebaseFirestoreTypes { * to the location. The document at the referenced location may or may not exist. A `DocumentReference` can also be used * to create a `CollectionReference` to a subcollection. */ - export interface DocumentReference { + export interface DocumentReference { /** * The Firestore instance the document is in. This is useful for performing transactions, for example. */ @@ -212,7 +219,7 @@ export namespace FirebaseFirestoreTypes { /** * The Collection this `DocumentReference` belongs to. */ - parent: CollectionReference; + parent: CollectionReference; /** * A string representing the path of the referenced document (relative to the root of the database). @@ -263,7 +270,7 @@ export namespace FirebaseFirestoreTypes { * * @param options An object to configure the get behavior. */ - get(options?: GetOptions): Promise; + get(options?: GetOptions): Promise>; /** * Returns true if this DocumentReference is equal to the provided one. @@ -306,7 +313,7 @@ export namespace FirebaseFirestoreTypes { onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; - next?: (snapshot: DocumentSnapshot) => void; + next?: (snapshot: DocumentSnapshot) => void; }): () => void; /** @@ -338,7 +345,7 @@ export namespace FirebaseFirestoreTypes { observer: { complete?: () => void; error?: (error: Error) => void; - next?: (snapshot: DocumentSnapshot) => void; + next?: (snapshot: DocumentSnapshot) => void; }, ): () => void; @@ -365,7 +372,7 @@ export namespace FirebaseFirestoreTypes { * @param onCompletion An optional function which will never be called. */ onSnapshot( - onNext: (snapshot: DocumentSnapshot) => void, + onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void, ): () => void; @@ -396,7 +403,7 @@ export namespace FirebaseFirestoreTypes { */ onSnapshot( options: SnapshotListenOptions, - onNext: (snapshot: DocumentSnapshot) => void, + onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void, ): () => void; @@ -422,7 +429,7 @@ export namespace FirebaseFirestoreTypes { * @param data A map of the fields and values for the document. * @param options An object to configure the set behavior. */ - set(data: { [key: string]: value }, options?: SetOptions): Promise; + set(data: T, options?: SetOptions): Promise; /** * Updates fields in the document referred to by this `DocumentReference`. The update will fail @@ -441,7 +448,7 @@ export namespace FirebaseFirestoreTypes { * * @param data An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document. */ - update(data: { [key: string]: value }): Promise; + update(data: Partial<{ [K in keyof T]: T[K] | FieldValue }>): Promise; /** * Updates fields in the document referred to by this DocumentReference. The update will fail if @@ -460,7 +467,7 @@ export namespace FirebaseFirestoreTypes { * @param value The first value. * @param moreFieldsAndValues Additional key value pairs. */ - update(field: string | FieldPath, value: any, ...moreFieldsAndValues: any[]): Promise; + update(field: keyof T | FieldPath, value: any, ...moreFieldsAndValues: any[]): Promise; } /** @@ -470,7 +477,7 @@ export namespace FirebaseFirestoreTypes { * For a DocumentSnapshot that points to a non-existing document, any data access will return 'undefined'. * You can use the `exists` property to explicitly verify a document's existence. */ - export interface DocumentSnapshot { + export interface DocumentSnapshot { /** * Property of the `DocumentSnapshot` that signals whether or not the data exists. True if the document exists. */ @@ -489,7 +496,7 @@ export namespace FirebaseFirestoreTypes { /** * The `DocumentReference` for the document included in the `DocumentSnapshot`. */ - ref: DocumentReference; + ref: DocumentReference; /** * Retrieves all fields in the document as an Object. Returns 'undefined' if the document doesn't exist. @@ -502,7 +509,7 @@ export namespace FirebaseFirestoreTypes { * console.log('User', user.data()); * ``` */ - data(): { [key: string]: value } | undefined; + data(): T | undefined; /** * Retrieves the field specified by fieldPath. Returns undefined if the document or field doesn't exist. @@ -517,7 +524,7 @@ export namespace FirebaseFirestoreTypes { * * @param fieldPath The path (e.g. 'foo' or 'foo.bar') to a specific field. */ - get(fieldPath: string | FieldPath): fieldType; + get(fieldPath: keyof T | FieldPath): fieldType; /** * Returns true if this `DocumentSnapshot` is equal to the provided one. @@ -544,7 +551,8 @@ export namespace FirebaseFirestoreTypes { * A QueryDocumentSnapshot offers the same API surface as a DocumentSnapshot. * Since query results contain only existing documents, the exists property will always be true and data() will never return 'undefined'. */ - export interface QueryDocumentSnapshot extends DocumentSnapshot { + export interface QueryDocumentSnapshot + extends DocumentSnapshot { /** * A QueryDocumentSnapshot is always guaranteed to exist. */ @@ -563,7 +571,7 @@ export namespace FirebaseFirestoreTypes { * } * ``` */ - data(): { [key: string]: value }; + data(): T; } /** @@ -827,7 +835,7 @@ export namespace FirebaseFirestoreTypes { * A Query refers to a `Query` which you can read or listen to. You can also construct refined `Query` objects by * adding filters and ordering. */ - export interface Query { + export interface Query { /** * Creates and returns a new Query that ends at the provided document (inclusive). The end * position is relative to the order of the query. The document must contain all of the @@ -849,7 +857,7 @@ export namespace FirebaseFirestoreTypes { * * @param snapshot The snapshot of the document to end at. */ - endAt(snapshot: DocumentSnapshot): Query; + endAt(snapshot: DocumentSnapshot): Query; /** * Creates and returns a new Query that ends at the provided fields relative to the order of the query. @@ -867,7 +875,7 @@ export namespace FirebaseFirestoreTypes { * * @param fieldValues The field values to end this query at, in order of the query's order by. */ - endAt(...fieldValues: any[]): Query; + endAt(...fieldValues: any[]): Query; /** * Creates and returns a new Query that ends before the provided document (exclusive). The end @@ -890,7 +898,7 @@ export namespace FirebaseFirestoreTypes { * * @param snapshot The snapshot of the document to end before. */ - endBefore(snapshot: DocumentSnapshot): Query; + endBefore(snapshot: DocumentSnapshot): Query; /** * Creates and returns a new Query that ends before the provided fields relative to the order of @@ -908,7 +916,7 @@ export namespace FirebaseFirestoreTypes { * * @param fieldValues The field values to end this query before, in order of the query's order by. */ - endBefore(...fieldValues: any[]): Query; + endBefore(...fieldValues: any[]): Query; /** * Executes the query and returns the results as a QuerySnapshot. @@ -930,7 +938,7 @@ export namespace FirebaseFirestoreTypes { * * @param options An object to configure the get behavior. */ - get(options?: GetOptions): Promise; + get(options?: GetOptions): Promise>; /** * Returns true if this Query is equal to the provided one. @@ -970,7 +978,8 @@ export namespace FirebaseFirestoreTypes { * * @param limit The maximum number of items to return. */ - limit(limit: number): Query; + limit(limit: number): Query; + /** * Creates and returns a new Query where the results are limited to the specified number of documents * starting from the last document. The order is dependent on the second parameter for the `orderBy` @@ -989,7 +998,7 @@ export namespace FirebaseFirestoreTypes { * * @param limitToLast The maximum number of items to return. */ - limitToLast(limitToLast: number): Query; + limitToLast(limitToLast: number): Query; /** * Attaches a listener for `QuerySnapshot` events. @@ -1015,7 +1024,7 @@ export namespace FirebaseFirestoreTypes { onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; - next?: (snapshot: QuerySnapshot) => void; + next?: (snapshot: QuerySnapshot) => void; }): () => void; /** @@ -1047,7 +1056,7 @@ export namespace FirebaseFirestoreTypes { observer: { complete?: () => void; error?: (error: Error) => void; - next?: (snapshot: QuerySnapshot) => void; + next?: (snapshot: QuerySnapshot) => void; }, ): () => void; @@ -1074,7 +1083,7 @@ export namespace FirebaseFirestoreTypes { * @param onCompletion An optional function which will never be called. */ onSnapshot( - onNext: (snapshot: QuerySnapshot) => void, + onNext: (snapshot: QuerySnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void, ): () => void; @@ -1105,7 +1114,7 @@ export namespace FirebaseFirestoreTypes { */ onSnapshot( options: SnapshotListenOptions, - onNext: (snapshot: QuerySnapshot) => void, + onNext: (snapshot: QuerySnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void, ): () => void; @@ -1128,7 +1137,7 @@ export namespace FirebaseFirestoreTypes { * @param fieldPath The field to sort by. Either a string or FieldPath instance. * @param directionStr Optional direction to sort by (`asc` or `desc`). If not specified, order will be ascending. */ - orderBy(fieldPath: string | FieldPath, directionStr?: 'asc' | 'desc'): Query; + orderBy(fieldPath: keyof T | FieldPath, directionStr?: 'asc' | 'desc'): Query; /** * Creates and returns a new Query that starts after the provided document (exclusive). The start @@ -1152,7 +1161,7 @@ export namespace FirebaseFirestoreTypes { * * @param snapshot The snapshot of the document to start after. */ - startAfter(snapshot: DocumentSnapshot): Query; + startAfter(snapshot: DocumentSnapshot): Query; /** * Creates and returns a new Query that starts after the provided fields relative to the order of @@ -1171,7 +1180,7 @@ export namespace FirebaseFirestoreTypes { * * @param fieldValues The field values to start this query after, in order of the query's order by. */ - startAfter(...fieldValues: any[]): Query; + startAfter(...fieldValues: any[]): Query; /** * Creates and returns a new Query that starts at the provided document (inclusive). The start @@ -1195,7 +1204,7 @@ export namespace FirebaseFirestoreTypes { * * @param snapshot The snapshot of the document to start at. */ - startAt(snapshot: DocumentSnapshot): Query; + startAt(snapshot: DocumentSnapshot): Query; /** * Creates and returns a new Query that starts at the provided fields relative to the order of the query. @@ -1214,7 +1223,7 @@ export namespace FirebaseFirestoreTypes { * * @param fieldValues The field values to start this query at, in order of the query's order by. */ - startAt(...fieldValues: any[]): Query; + startAt(...fieldValues: any[]): Query; /** * Creates and returns a new Query with the additional filter that documents must contain the specified field and @@ -1234,7 +1243,7 @@ export namespace FirebaseFirestoreTypes { * @param opStr The operation string (e.g "<", "<=", "==", ">", ">=", "array-contains", "array-contains-any", "in"). * @param value The comparison value. */ - where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: any): Query; + where(fieldPath: keyof T | FieldPath, opStr: WhereFilterOp, value: any): Query; } /** @@ -1255,11 +1264,11 @@ export namespace FirebaseFirestoreTypes { * can be accessed as an array via the `docs` property or enumerated using the `forEach` method. The number of documents * can be determined via the `empty` and `size` properties. */ - export interface QuerySnapshot { + export interface QuerySnapshot { /** * An array of all the documents in the `QuerySnapshot`. */ - docs: QueryDocumentSnapshot[]; + docs: QueryDocumentSnapshot[]; /** * True if there are no documents in the `QuerySnapshot`. @@ -1274,7 +1283,7 @@ export namespace FirebaseFirestoreTypes { /** * The query on which you called get or `onSnapshot` in order to `get` this `QuerySnapshot`. */ - query: Query; + query: Query; /** * The number of documents in the `QuerySnapshot`. @@ -1309,7 +1318,7 @@ export namespace FirebaseFirestoreTypes { * * @param options `SnapshotListenOptions` that control whether metadata-only changes (i.e. only `DocumentSnapshot.metadata` changed) should trigger snapshot events. */ - docChanges(options?: SnapshotListenOptions): DocumentChange[]; + docChanges(options?: SnapshotListenOptions): DocumentChange[]; /** * Enumerates all of the documents in the `QuerySnapshot`. @@ -1328,7 +1337,10 @@ export namespace FirebaseFirestoreTypes { * @param thisArg The `this` binding for the callback. */ - forEach(callback: (result: QueryDocumentSnapshot, index: number) => void, thisArg?: any): void; + forEach( + callback: (result: QueryDocumentSnapshot, index: number) => void, + thisArg?: any, + ): void; /** * Returns true if this `QuerySnapshot` is equal to the provided one. @@ -1561,7 +1573,9 @@ export namespace FirebaseFirestoreTypes { * * @param documentRef A reference to the document to be read. */ - get(documentRef: DocumentReference): Promise; + get( + documentRef: DocumentReference, + ): Promise>; /** * Writes to the document referred to by the provided `DocumentReference`. If the document does not exist yet, @@ -1587,9 +1601,9 @@ export namespace FirebaseFirestoreTypes { * @param data An object of the fields and values for the document. * @param options An object to configure the set behavior. */ - set( - documentRef: DocumentReference, - data: { [key: string]: value }, + set( + documentRef: DocumentReference, + data: T, options?: SetOptions, ): Transaction; @@ -1614,7 +1628,10 @@ export namespace FirebaseFirestoreTypes { * @param documentRef A reference to the document to be updated. * @param data An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document. */ - update(documentRef: DocumentReference, data: { [key: string]: value }): Transaction; + update( + documentRef: DocumentReference, + data: Partial<{ [K in keyof T]: T[K] | FieldValue }>, + ): Transaction; /** * Updates fields in the document referred to by the provided DocumentReference. The update will fail if applied to @@ -1639,10 +1656,10 @@ export namespace FirebaseFirestoreTypes { * @param value The first value. * @param moreFieldsAndValues Additional key/value pairs. */ - update( - documentRef: DocumentReference, - field: string | FieldPath, - value: any, + update( + documentRef: DocumentReference, + field: K | FieldPath, + value: T[K], ...moreFieldsAndValues: any[] ): Transaction; } @@ -1713,9 +1730,9 @@ export namespace FirebaseFirestoreTypes { * @param data An object of the fields and values for the document. * @param options An object to configure the set behavior. */ - set( - documentRef: DocumentReference, - data: { [key: string]: value }, + set( + documentRef: DocumentReference, + data: T, options?: SetOptions, ): WriteBatch; @@ -1736,7 +1753,10 @@ export namespace FirebaseFirestoreTypes { * @param documentRef A reference to the document to be updated. * @param data An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document. */ - update(documentRef: DocumentReference, data: { [key: string]: value }): WriteBatch; + update( + documentRef: DocumentReference, + data: Partial<{ [K in keyof T]: T[K] | FieldValue }>, + ): WriteBatch; /** * Updates fields in the document referred to by this DocumentReference. The update will fail if applied to a document that does not exist. @@ -1757,10 +1777,10 @@ export namespace FirebaseFirestoreTypes { * @param value The first value. * @param moreFieldAndValues Additional key value pairs. */ - update( - documentRef: DocumentReference, - field: string | FieldPath, - value: any, + update( + documentRef: DocumentReference, + field: K | FieldPath, + value: T[K] | FieldValue, ...moreFieldAndValues: any[] ): WriteBatch; } @@ -1862,7 +1882,9 @@ export namespace FirebaseFirestoreTypes { * * @param collectionPath A slash-separated path to a collection. */ - collection(collectionPath: string): CollectionReference; + collection( + collectionPath: string, + ): CollectionReference; /** * Creates and returns a new Query that includes all documents in the database that are contained @@ -1876,7 +1898,7 @@ export namespace FirebaseFirestoreTypes { * * @param collectionId Identifies the collections to query over. Every collection or subcollection with this ID as the last segment of its path will be included. Cannot contain a slash. */ - collectionGroup(collectionId: string): Query; + collectionGroup(collectionId: string): Query; /** * Disables network usage for this instance. It can be re-enabled via `enableNetwork()`. While the @@ -1904,7 +1926,7 @@ export namespace FirebaseFirestoreTypes { * * @param documentPath A slash-separated path to a document. */ - doc(documentPath: string): DocumentReference; + doc(documentPath: string): DocumentReference; /** * Re-enables use of the network for this Firestore instance after a prior call to `disableNetwork()`.