Skip to content

Commit

Permalink
fix: exactOptionalPropertyTypes TS option compatibility (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Nov 25, 2023
1 parent e58ebf8 commit 581774a
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 76 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@semantic-release/git": "10.0.1",
"@testing-library/react": "14.0.0",
"@tsconfig/recommended": "1.0.3",
"@tsconfig/strictest": "2.0.2",
"@types/react": "18.2.33",
"@typescript-eslint/eslint-plugin": "6.9.0",
"@typescript-eslint/parser": "6.9.0",
Expand Down
6 changes: 3 additions & 3 deletions src/database/useObjectValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type UseObjectValueResult<Value = unknown> = ValueHookResult<Value, Error
export type UseObjectValueConverter<Value> = (snap: DataSnapshot) => Value;

export interface UseObjectValueOptions<Value> {
converter?: UseObjectValueConverter<Value>;
initialValue?: Value;
converter?: UseObjectValueConverter<Value> | undefined;
initialValue?: Value | undefined;
}

/**
Expand All @@ -28,7 +28,7 @@ export interface UseObjectValueOptions<Value> {
*/
export function useObjectValue<Value = unknown>(
query: Query | undefined | null,
options?: UseObjectValueOptions<Value>,
options?: UseObjectValueOptions<Value> | undefined,
): UseObjectValueResult<Value> {
const { converter = defaultConverter, initialValue = LoadingState } = options ?? {};

Expand Down
4 changes: 2 additions & 2 deletions src/database/useObjectValueOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type UseObjectValueOnceResult<Value = unknown> = ValueHookResult<Value, E
export type UseObjectValueOnceConverter<Value> = (snap: DataSnapshot) => Value;

export interface UseObjectValueOnceOptions<Value> {
converter?: UseObjectValueOnceConverter<Value>;
converter?: UseObjectValueOnceConverter<Value> | undefined;
}

/**
Expand All @@ -25,7 +25,7 @@ export interface UseObjectValueOnceOptions<Value> {
*/
export function useObjectValueOnce<Value = unknown>(
query: Query | undefined | null,
options?: UseObjectValueOnceOptions<Value>,
options?: UseObjectValueOnceOptions<Value> | undefined,
): UseObjectValueOnceResult<Value> {
const { converter = defaultConverter } = options ?? {};

Expand Down
7 changes: 7 additions & 0 deletions src/firestore/internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ describe("isAggregateSpecEqual", () => {
expect(isAggregateSpecEqual(spec1, spec2)).toBe(false);
});

it("different keys", () => {
const spec1 = { count: count() };
const spec2 = { total: count() };
// @ts-expect-error Different specs
expect(isAggregateSpecEqual(spec1, spec2)).toBe(false);
});

it("different aggregations", () => {
const spec1 = { value: count() };
const spec2 = { value: average("abc") };
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ export function isAggregateSpecEqual<T extends AggregateSpec>(a: T, b: T): boole
return false;
}

return Object.entries(a).every(([key, value]) => aggregateFieldEqual(value, b[key]));
return Object.entries(a).every(([key, value]) => aggregateFieldEqual(value, b[key]!));
}
6 changes: 3 additions & 3 deletions src/firestore/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type UseDocumentResult<AppModelType = DocumentData> = ValueHookResult<Doc
* Options to configure the subscription
*/
export interface UseDocumentOptions {
snapshotListenOptions?: SnapshotListenOptions;
snapshotListenOptions?: SnapshotListenOptions | undefined;
}

/**
Expand All @@ -34,10 +34,10 @@ export interface UseDocumentOptions {
*/
export function useDocument<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
reference: DocumentReference<AppModelType, DbModelType> | undefined | null,
options?: UseDocumentOptions,
options?: UseDocumentOptions | undefined,
): UseDocumentResult<AppModelType> {
const { snapshotListenOptions } = options ?? {};
const { includeMetadataChanges } = snapshotListenOptions ?? {};
const { includeMetadataChanges = false } = snapshotListenOptions ?? {};

const onChange: UseListenOnChange<
DocumentSnapshot<AppModelType, DbModelType>,
Expand Down
12 changes: 6 additions & 6 deletions src/firestore/useDocumentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export type UseDocumentDataResult<AppModelType = DocumentData> = ValueHookResult
* Options to configure the subscription
*/
export interface UseDocumentDataOptions<AppModelType = DocumentData> {
snapshotListenOptions?: SnapshotListenOptions;
snapshotOptions?: SnapshotOptions;
initialValue?: AppModelType;
snapshotListenOptions?: SnapshotListenOptions | undefined;
snapshotOptions?: SnapshotOptions | undefined;
initialValue?: AppModelType | undefined;
}

/**
Expand All @@ -37,11 +37,11 @@ export interface UseDocumentDataOptions<AppModelType = DocumentData> {
*/
export function useDocumentData<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
reference: DocumentReference<AppModelType, DbModelType> | undefined | null,
options?: UseDocumentDataOptions<AppModelType>,
options?: UseDocumentDataOptions<AppModelType> | undefined,
): UseDocumentDataResult<AppModelType> {
const { snapshotListenOptions, snapshotOptions } = options ?? {};
const { includeMetadataChanges } = snapshotListenOptions ?? {};
const { serverTimestamps } = snapshotOptions ?? {};
const { includeMetadataChanges = false } = snapshotListenOptions ?? {};
const { serverTimestamps = "none" } = snapshotOptions ?? {};

const onChange: UseListenOnChange<
AppModelType,
Expand Down
8 changes: 4 additions & 4 deletions src/firestore/useDocumentDataOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type UseDocumentDataOnceResult<AppModelType = DocumentData> = ValueHookRe
* Options to configure how the document is fetched
*/
export interface UseDocumentDataOnceOptions {
source?: Source;
snapshotOptions?: SnapshotOptions;
source?: Source | undefined;
snapshotOptions?: SnapshotOptions | undefined;
}

/**
Expand All @@ -28,10 +28,10 @@ export interface UseDocumentDataOnceOptions {
*/
export function useDocumentDataOnce<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
reference: DocumentReference<AppModelType, DbModelType> | undefined | null,
options?: UseDocumentDataOnceOptions,
options?: UseDocumentDataOnceOptions | undefined,
): UseDocumentDataOnceResult<AppModelType> {
const { source = "default", snapshotOptions } = options ?? {};
const { serverTimestamps } = snapshotOptions ?? {};
const { serverTimestamps = "none" } = snapshotOptions ?? {};

const getData = useCallback(
async (stableRef: DocumentReference<AppModelType, DbModelType>) => {
Expand Down
4 changes: 2 additions & 2 deletions src/firestore/useDocumentOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type UseDocumentOnceResult<AppModelType = DocumentData> = ValueHookResult
* Options to configure how the document is fetched
*/
export interface UseDocumentOnceOptions {
source?: Source;
source?: Source | undefined;
}

/**
Expand All @@ -30,7 +30,7 @@ export interface UseDocumentOnceOptions {
*/
export function useDocumentOnce<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
reference: DocumentReference<AppModelType, DbModelType> | undefined | null,
options?: UseDocumentOnceOptions,
options?: UseDocumentOnceOptions | undefined,
): UseDocumentOnceResult<AppModelType> {
const { source = "default" } = options ?? {};

Expand Down
6 changes: 3 additions & 3 deletions src/firestore/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type UseQueriesResult<AppModelTypes extends ReadonlyArray<unknown> = Read
* Options to configure the subscription
*/
export interface UseQueriesOptions {
snapshotListenOptions?: SnapshotListenOptions;
snapshotListenOptions?: SnapshotListenOptions | undefined;
}

/**
Expand All @@ -31,10 +31,10 @@ export function useQueries<
DbModelTypes extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>,
>(
queries: { [Index in keyof AppModelTypes]: Query<AppModelTypes[Index], DbModelTypes[number]> },
options?: UseQueriesOptions,
options?: UseQueriesOptions | undefined,
): UseQueriesResult<AppModelTypes> {
const { snapshotListenOptions } = options ?? {};
const { includeMetadataChanges } = snapshotListenOptions ?? {};
const { includeMetadataChanges = false } = snapshotListenOptions ?? {};

const onChange: UseMultiListenChange<
QuerySnapshot<AppModelTypes[number], DbModelTypes[number]>,
Expand Down
10 changes: 5 additions & 5 deletions src/firestore/useQueriesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export type UseQueriesDataResult<AppModelTypes extends ReadonlyArray<unknown> =
* Options to configure the subscription
*/
export interface UseQueriesDataOptions {
snapshotListenOptions?: SnapshotListenOptions;
snapshotOptions?: SnapshotOptions;
snapshotListenOptions?: SnapshotListenOptions | undefined;
snapshotOptions?: SnapshotOptions | undefined;
}

/**
Expand All @@ -32,11 +32,11 @@ export function useQueriesData<
DbModelTypes extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>,
>(
queries: { [Index in keyof AppModelTypes]: Query<AppModelTypes[Index], DbModelTypes[number]> },
options?: UseQueriesDataOptions,
options?: UseQueriesDataOptions | undefined,
): UseQueriesDataResult<AppModelTypes> {
const { snapshotListenOptions, snapshotOptions } = options ?? {};
const { includeMetadataChanges } = snapshotListenOptions ?? {};
const { serverTimestamps } = snapshotOptions ?? {};
const { includeMetadataChanges = false } = snapshotListenOptions ?? {};
const { serverTimestamps = "none" } = snapshotOptions ?? {};

const onChange: UseMultiListenChange<
AppModelTypes[number],
Expand Down
8 changes: 4 additions & 4 deletions src/firestore/useQueriesDataOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type UseQueriesDataOnceResult<AppModelTypes extends ReadonlyArray<unknown
* Options to configure the subscription
*/
export interface UseQueriesDataOnceOptions {
source?: Source;
snapshotOptions?: SnapshotOptions;
source?: Source | undefined;
snapshotOptions?: SnapshotOptions | undefined;
}

/**
Expand All @@ -33,10 +33,10 @@ export function useQueriesDataOnce<
DbModelTypes extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>,
>(
queries: { [Index in keyof AppModelTypes]: Query<AppModelTypes[Index], DbModelTypes[number]> },
options?: UseQueriesDataOnceOptions,
options?: UseQueriesDataOnceOptions | undefined,
): UseQueriesDataOnceResult<AppModelTypes> {
const { source = "default", snapshotOptions } = options ?? {};
const { serverTimestamps } = snapshotOptions ?? {};
const { serverTimestamps = "none" } = snapshotOptions ?? {};

const getData = useCallback(
async (stableQuery: Query<AppModelTypes[number], DbModelTypes[number]>) => {
Expand Down
6 changes: 3 additions & 3 deletions src/firestore/useQueriesOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type UseQueriesOnceResult<AppModelTypes extends ReadonlyArray<unknown> =
* Options to configure the subscription
*/
export interface UseQueriesOnceOptions {
source?: Source;
snapshotOptions?: SnapshotOptions;
source?: Source | undefined;
snapshotOptions?: SnapshotOptions | undefined;
}

/**
Expand All @@ -33,7 +33,7 @@ export function useQueriesOnce<
DbModelTypes extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>,
>(
queries: { [Index in keyof AppModelTypes]: Query<AppModelTypes[Index], DbModelTypes[number]> },
options?: UseQueriesOnceOptions,
options?: UseQueriesOnceOptions | undefined,
): UseQueriesOnceResult<AppModelTypes> {
const { source = "default" } = options ?? {};

Expand Down
6 changes: 3 additions & 3 deletions src/firestore/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type UseQueryResult<AppModelType = DocumentData> = ValueHookResult<QueryS
* Options to configure the subscription
*/
export interface UseQueryOptions {
snapshotListenOptions?: SnapshotListenOptions;
snapshotListenOptions?: SnapshotListenOptions | undefined;
}

/**
Expand All @@ -27,10 +27,10 @@ export interface UseQueryOptions {
*/
export function useQuery<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
query: Query<AppModelType, DbModelType> | undefined | null,
options?: UseQueryOptions,
options?: UseQueryOptions | undefined,
): UseQueryResult<AppModelType> {
const { snapshotListenOptions } = options ?? {};
const { includeMetadataChanges } = snapshotListenOptions ?? {};
const { includeMetadataChanges = false } = snapshotListenOptions ?? {};

const onChange: UseListenOnChange<
QuerySnapshot<AppModelType, DbModelType>,
Expand Down
21 changes: 7 additions & 14 deletions src/firestore/useQueryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type UseQueryDataResult<AppModelType = DocumentData> = ValueHookResult<Ap
* Options to configure the subscription
*/
export interface UseQueryDataOptions<AppModelType = DocumentData> {
snapshotListenOptions?: SnapshotListenOptions;
snapshotOptions?: SnapshotOptions;
initialValue?: AppModelType[];
snapshotListenOptions?: SnapshotListenOptions | undefined;
snapshotOptions?: SnapshotOptions | undefined;
initialValue?: AppModelType[] | undefined;
}

/**
Expand All @@ -30,26 +30,19 @@ export interface UseQueryDataOptions<AppModelType = DocumentData> {
*/
export function useQueryData<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
query: Query<AppModelType, DbModelType> | undefined | null,
options?: UseQueryDataOptions<AppModelType>,
options?: UseQueryDataOptions<AppModelType> | undefined,
): UseQueryDataResult<AppModelType> {
const { snapshotListenOptions, snapshotOptions } = options ?? {};
const { includeMetadataChanges } = snapshotListenOptions ?? {};
const { serverTimestamps } = snapshotOptions ?? {};
const { includeMetadataChanges = false } = snapshotListenOptions ?? {};
const { serverTimestamps = "none" } = snapshotOptions ?? {};

const onChange: UseListenOnChange<AppModelType[], FirestoreError, Query<AppModelType, DbModelType>> = useCallback(
(stableQuery, next, error) =>
onSnapshot(
stableQuery,
{ includeMetadataChanges },
{
next: (snap) =>
next(
snap.docs.map((doc) =>
doc.data({
serverTimestamps,
}),
),
),
next: (snap) => next(snap.docs.map((doc) => doc.data({ serverTimestamps }))),
error,
},
),
Expand Down
8 changes: 4 additions & 4 deletions src/firestore/useQueryDataOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type UseQueryDataOnceResult<AppModelType = DocumentData> = ValueHookResul
* Options to configure the subscription
*/
export interface UseQueryDataOnceOptions {
source?: Source;
snapshotOptions?: SnapshotOptions;
source?: Source | undefined;
snapshotOptions?: SnapshotOptions | undefined;
}

/**
Expand All @@ -28,10 +28,10 @@ export interface UseQueryDataOnceOptions {
*/
export function useQueryDataOnce<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
query: Query<AppModelType, DbModelType> | undefined | null,
options?: UseQueryDataOnceOptions,
options?: UseQueryDataOnceOptions | undefined,
): UseQueryDataOnceResult<AppModelType> {
const { source = "default", snapshotOptions } = options ?? {};
const { serverTimestamps } = snapshotOptions ?? {};
const { serverTimestamps = "none" } = snapshotOptions ?? {};

const getData = useCallback(
async (stableQuery: Query<AppModelType, DbModelType>) => {
Expand Down
4 changes: 2 additions & 2 deletions src/firestore/useQueryOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type UseQueryOnceResult<AppModelType = DocumentData> = ValueHookResult<Qu
* Options to configure how the query is fetched
*/
export interface UseQueryOnceOptions {
source?: Source;
source?: Source | undefined;
}

/**
Expand All @@ -27,7 +27,7 @@ export interface UseQueryOnceOptions {
*/
export function useQueryOnce<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
query: Query<AppModelType, DbModelType> | undefined | null,
options?: UseQueryOnceOptions,
options?: UseQueryOnceOptions | undefined,
): UseQueryOnceResult<AppModelType> {
const { source = "default" } = options ?? {};

Expand Down

0 comments on commit 581774a

Please sign in to comment.