@firebase/firestore-types provides comprehensive TypeScript type definitions for Firebase Firestore, serving as the foundational typing system for the Firebase JavaScript SDK's Firestore functionality. This package establishes type contracts for document operations, queries, real-time listeners, transactions, and offline capabilities, ensuring type safety across the entire Firebase ecosystem.
npm install @firebase/firestore-types (usually installed as a dependency)import type {
FirebaseFirestore,
DocumentData,
DocumentReference,
CollectionReference,
Query
} from "@firebase/firestore-types";Note: This package contains only TypeScript type definitions. In practice, these types are used with the actual Firebase SDK implementations.
This package is typically used for type annotations when working with Firebase Firestore:
import type { DocumentData, DocumentReference } from "@firebase/firestore-types";
// Using with Firestore operations
function saveDocument(
docRef: DocumentReference<DocumentData>,
data: DocumentData
): Promise<void> {
return docRef.set(data);
}The type definitions are organized around several key concepts:
FirebaseFirestore, DocumentData, UpdateData - fundamental database operationsDocumentReference, CollectionReference - typed references to database locationsQuery, QuerySnapshot - type-safe query building and resultsDocumentSnapshot, QueryDocumentSnapshot - typed data retrievalGeoPoint, Timestamp, Blob, FieldValue - Firestore-specific data typesTransaction, WriteBatch - batch and atomic operationsSettings, PersistenceSettings - database configurationFieldPath, error types, and various options interfacesFundamental types for database connections, settings, and basic operations.
class FirebaseFirestore {
settings(settings: Settings): void;
useEmulator(host: string, port: number, options?: { mockUserToken?: EmulatorMockTokenOptions | string; }): void;
collection(collectionPath: string): CollectionReference<DocumentData>;
doc(documentPath: string): DocumentReference<DocumentData>;
}
interface Settings {
host?: string;
ssl?: boolean;
cacheSizeBytes?: number;
experimentalForceLongPolling?: boolean;
experimentalAutoDetectLongPolling?: boolean;
ignoreUndefinedProperties?: boolean;
merge?: boolean;
}Types for document references and document-level operations including reads, writes, and real-time listeners.
class DocumentReference<T = DocumentData> {
readonly id: string;
readonly path: string;
set(data: T): Promise<void>;
set(data: Partial<T>, options: SetOptions): Promise<void>;
update(data: UpdateData): Promise<void>;
delete(): Promise<void>;
get(options?: GetOptions): Promise<DocumentSnapshot<T>>;
onSnapshot(observer: { next?: (snapshot: DocumentSnapshot<T>) => void; }): () => void;
}Types for collection references, query building, and query result handling.
class CollectionReference<T = DocumentData> extends Query<T> {
readonly id: string;
readonly path: string;
doc(documentPath?: string): DocumentReference<T>;
add(data: T): Promise<DocumentReference<T>>;
}
class Query<T = DocumentData> {
where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: any): Query<T>;
orderBy(fieldPath: string | FieldPath, directionStr?: OrderByDirection): Query<T>;
limit(limit: number): Query<T>;
get(options?: GetOptions): Promise<QuerySnapshot<T>>;
}Types for document and query snapshots that represent data retrieved from Firestore.
class DocumentSnapshot<T = DocumentData> {
readonly exists: boolean;
readonly id: string;
readonly metadata: SnapshotMetadata;
data(options?: SnapshotOptions): T | undefined;
get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
}
class QuerySnapshot<T = DocumentData> {
readonly docs: Array<QueryDocumentSnapshot<T>>;
readonly size: number;
readonly empty: boolean;
docChanges(options?: SnapshotListenOptions): Array<DocumentChange<T>>;
}Specialized data types for Firestore including timestamps, geographic points, binary data, and field values.
class Timestamp {
constructor(seconds: number, nanoseconds: number);
static now(): Timestamp;
static fromDate(date: Date): Timestamp;
toDate(): Date;
toMillis(): number;
}
class GeoPoint {
constructor(latitude: number, longitude: number);
readonly latitude: number;
readonly longitude: number;
}
class FieldValue {
static serverTimestamp(): FieldValue;
static delete(): FieldValue;
static arrayUnion(...elements: any[]): FieldValue;
static increment(n: number): FieldValue;
}Types for atomic operations, transactions, and batch writes.
class Transaction {
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
delete(documentRef: DocumentReference<any>): Transaction;
}
class WriteBatch {
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
delete(documentRef: DocumentReference<any>): WriteBatch;
commit(): Promise<void>;
}type DocumentData = { [field: string]: any };
type UpdateData = { [fieldPath: string]: any };
type LogLevel = 'debug' | 'error' | 'silent' | 'warn' | 'info' | 'verbose';interface FirestoreError {
code: FirestoreErrorCode;
message: string;
name: string;
stack?: string;
}
type FirestoreErrorCode =
| 'cancelled' | 'unknown' | 'invalid-argument' | 'deadline-exceeded'
| 'not-found' | 'already-exists' | 'permission-denied'
| 'resource-exhausted' | 'failed-precondition' | 'aborted'
| 'out-of-range' | 'unimplemented' | 'internal'
| 'unavailable' | 'data-loss' | 'unauthenticated';