Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/*.type-test.ts
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Firebase.

## Platform support

ReactFire is designed for **web React apps** and wraps the [Firebase Web SDK](https://firebase.google.com/docs/web/setup). It is not compatible with React Native or Expo. For React Native projects, use [react-native-firebase](https://rnfirebase.io/) instead.
ReactFire is designed for **web React apps** and wraps the [Firebase JavaScript SDK](https://firebase.google.com/docs/web/setup). It is not compatible with React Native or Expo. For React Native projects, use [react-native-firebase](https://rnfirebase.io/) instead.

## Install

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/functions/checkIdField.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/reference/functions/checkOptions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/reference/functions/checkinitialData.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/functions/useFirestoreCollection.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/functions/useFirestoreDoc.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/functions/useFirestoreDocOnce.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/reference/interfaces/ReactFireOptions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/reference/interfaces/SignInCheckOptionsBasic.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/reference/interfaces/SignInCheckOptionsClaimsObject.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/firestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function getDocObservableId<T = DocumentData>(ref: DocumentReference<T>) {
*
* You can preload data for this hook by calling `preloadFirestoreDoc`
*/
export function useFirestoreDoc<T = DocumentData>(ref: DocumentReference<T>, options?: ReactFireOptions<T>): ObservableStatus<DocumentSnapshot<T>> {
export function useFirestoreDoc<T = DocumentData>(ref: DocumentReference<T>, options?: ReactFireOptions<DocumentSnapshot<T>>): ObservableStatus<DocumentSnapshot<T>> {
const observableId = getDocObservableId(ref);
const observable$ = doc(ref);

Expand All @@ -49,7 +49,7 @@ export function useFirestoreDoc<T = DocumentData>(ref: DocumentReference<T>, opt
/**
* Get a firestore document and don't subscribe to changes
*/
export function useFirestoreDocOnce<T = DocumentData>(ref: DocumentReference<T>, options?: ReactFireOptions<T>): ObservableStatus<DocumentSnapshot<T>> {
export function useFirestoreDocOnce<T = DocumentData>(ref: DocumentReference<T>, options?: ReactFireOptions<DocumentSnapshot<T>>): ObservableStatus<DocumentSnapshot<T>> {
const observableId = `firestore:docOnce:${ref.firestore.app.name}:${ref.path}`;
const observable$ = doc(ref).pipe(first());

Expand All @@ -63,7 +63,7 @@ export function useFirestoreDocData<T = unknown>(ref: DocumentReference<T>, opti
const idField = options ? checkIdField(options) : undefined;

const observableId = `firestore:docData:${ref.firestore.app.name}:${ref.path}:idField=${JSON.stringify(idField)}`;
const observable = docData(ref, { idField });
const observable = docData(ref, { idField: idField as keyof T });

return useObservable(observableId, observable, options) as ObservableStatus<T>;
}
Expand All @@ -75,15 +75,15 @@ export function useFirestoreDocDataOnce<T = unknown>(ref: DocumentReference<T>,
const idField = options ? checkIdField(options) : undefined;

const observableId = `firestore:docDataOnce:${ref.firestore.app.name}:${ref.path}:idField=${JSON.stringify(idField)}`;
const observable$ = docData(ref, { idField }).pipe(first());
const observable$ = docData(ref, { idField: idField as keyof T }).pipe(first());

return useObservable(observableId, observable$, options) as ObservableStatus<T>;
}

/**
* Subscribe to a Firestore collection
*/
export function useFirestoreCollection<T = DocumentData>(query: FirestoreQuery<T>, options?: ReactFireOptions<T[]>): ObservableStatus<QuerySnapshot<T>> {
export function useFirestoreCollection<T = DocumentData>(query: FirestoreQuery<T>, options?: ReactFireOptions<QuerySnapshot<T>>): ObservableStatus<QuerySnapshot<T>> {
const observableId = `firestore:collection:${getUniqueIdForFirestoreQuery(query)}`;
const observable$ = fromRef(query);

Expand All @@ -96,7 +96,7 @@ export function useFirestoreCollection<T = DocumentData>(query: FirestoreQuery<T
export function useFirestoreCollectionData<T = DocumentData>(query: FirestoreQuery<T>, options?: ReactFireOptions<T[]>): ObservableStatus<T[]> {
const idField = options ? checkIdField(options) : undefined;
const observableId = `firestore:collectionData:${getUniqueIdForFirestoreQuery(query)}:idField=${JSON.stringify(idField)}`;
const observable$ = collectionData(query, { idField });
const observable$ = collectionData(query, { idField: idField as (string & keyof T) });

return useObservable(observableId, observable$, options);
}
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ export class ReactFireError extends Error {

export interface ReactFireOptions<T = unknown> {
idField?: string;
initialData?: T | any;
initialData?: T;
/**
* @deprecated use initialData instead
*/
startWithValue?: T | any;
startWithValue?: T;
suspense?: boolean;
}

// Deprecated: unused internally as of the ReactFireOptions generic tightening.
// Removal is tracked in #754 alongside the other deprecated exports, so it
// lands as its own change rather than inside this one.
export function checkOptions(options: ReactFireOptions, field: string) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any of these are actually used, and agree deleting is the right thing, but this is technically a breaking change. Let's leave this for now, since it isn't hurting anything (unless it is, in which case let me know)

Please add an issue reminding us to remove all of these in v5

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Both are kept as exports (no breaking removal), with a comment marking them unused-and-slated-for-v5. Filed #754 to remove them.

One heads-up: tightening initialData to T shifts checkinitialData's inferred return type from any to unknown (visible in its regenerated doc). It's unused internally and on the v5 chopping block, so I left it rather than adding a cast to a dead export, but flagging in case you'd rather I pin it.

@tyler-reitz tyler-reitz Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note it now also covers startWithValue, ClaimsCheck and AuthCheck; startWithValue is not type-only, useObservable reads it as an initialData fallback.

// make sure the field passed in is a valid key of ReactFire Options
if (field === 'idField' || field === 'initialData' || field === 'suspense') {
Expand All @@ -44,8 +47,8 @@ export function checkinitialData(options: ReactFireOptions) {
return checkOptions(options, 'initialData');
}

export function checkIdField(options: ReactFireOptions) {
return checkOptions(options, 'idField');
export function checkIdField(options: ReactFireOptions): string | undefined {
return options?.idField;
}

export * from './auth';
Expand Down
46 changes: 46 additions & 0 deletions src/reactfire-options.type-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Type-level regression tests for ReactFireOptions<T> generic constraints.
* Checked by `tsc --noEmit` in CI. No runtime behavior — not bundled.
*/
import type { ReactFireOptions } from './index';

// ---- initialData must match T ----

void ((): ReactFireOptions<string> => ({ initialData: 'hello' }))();
void ((): ReactFireOptions<number> => ({ initialData: 42 }))();

// @ts-expect-error initialData must be T, not a different type
const _wrongInitialData: ReactFireOptions<string> = { initialData: 123 };
void _wrongInitialData;

// @ts-expect-error startWithValue must be T, not a different type
const _wrongStartWithValue: ReactFireOptions<string> = { startWithValue: 123 };
void _wrongStartWithValue;

// ---- snapshot hooks take the snapshot, data hooks take the data (#741) ----
//
// Removing `T | any` also removed what was masking a mismatch: the raw snapshot
// hooks resolve to a DocumentSnapshot/QuerySnapshot, so that is what a correct
// initialData is. Without the hook-level fix, seeding them with a snapshot (the
// only value you can actually have) stops compiling.

import type { DocumentReference, DocumentSnapshot, Query, QuerySnapshot } from 'firebase/firestore';
import type { useFirestoreCollection, useFirestoreDoc, useFirestoreDocData } from './firestore';

declare const ref: DocumentReference<{ a: string }>;
declare const query: Query<{ a: string }>;
declare const snap: DocumentSnapshot<{ a: string }>;
declare const querySnap: QuerySnapshot<{ a: string }>;
declare const docHook: typeof useFirestoreDoc;
declare const collectionHook: typeof useFirestoreCollection;
declare const dataHook: typeof useFirestoreDocData;

void (() => docHook(ref, { initialData: snap }));
void (() => collectionHook(query, { initialData: querySnap }));
void (() => dataHook(ref, { initialData: { a: 'x' } }));

// @ts-expect-error a snapshot hook must not accept the unwrapped data type
void (() => docHook(ref, { initialData: { a: 'x' } }));

// @ts-expect-error a data hook must not accept a snapshot
void (() => dataHook(ref, { initialData: snap }));
Loading