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
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.

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.

6 changes: 3 additions & 3 deletions src/firestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +75,7 @@ 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>;
}
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);
}
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ 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.
// Kept as exports to avoid a breaking removal; slated for removal in v5.
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.

// make sure the field passed in is a valid key of ReactFire Options
if (field === 'idField' || field === 'initialData' || field === 'suspense') {
Expand All @@ -44,8 +46,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
18 changes: 18 additions & 0 deletions src/reactfire-options.type-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 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;
Loading