Skip to content

Commit 67d0dae

Browse files
author
devgioele
committed
fix types of zod hooks
1 parent 653ab0b commit 67d0dae

4 files changed

Lines changed: 42 additions & 7 deletions

File tree

src/routers/__test__/nextRouter.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ describe('NextRouter', () => {
119119
expectTypeOf(resultPagination.current.page).toEqualTypeOf<number>()
120120
})
121121

122+
test('passing a partial default query should return the default query values as not undefined and the remaining query values as possibly undefined', () => {
123+
const querySchema = z.object({
124+
role: z.string(),
125+
name: z.string(),
126+
})
127+
128+
const { result: resultQueryAndPagination } = renderHook(() =>
129+
useQueryAndPagination(querySchema, { role: '' }),
130+
)
131+
expectTypeOf(
132+
resultQueryAndPagination.current.query.role,
133+
).toEqualTypeOf<string>()
134+
expectTypeOf(resultQueryAndPagination.current.query.name).toEqualTypeOf<
135+
string | undefined
136+
>()
137+
138+
const { result: resultQuery } = renderHook(() =>
139+
useQuery(querySchema, { name: '' }),
140+
)
141+
expectTypeOf(resultQuery.current.query.role).toEqualTypeOf<
142+
string | undefined
143+
>()
144+
expectTypeOf(resultQuery.current.query.name).toEqualTypeOf<string>()
145+
})
146+
122147
test('change default parameters', () => {
123148
const page = 1
124149
const size = 10

src/zod/routers/inMemory.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ export const useQuery = <
1313
schemaQuery: TSchema,
1414
defaultQuery: Partial<TQuery> = {},
1515
options?: Partial<AbstractQueryOptions>,
16-
) => useQueryVanilla(zodParser(schemaQuery), defaultQuery, options)
16+
) =>
17+
useQueryVanilla(
18+
zodParser<TQuery, TSchema>(schemaQuery),
19+
defaultQuery,
20+
options,
21+
)
1722

1823
export const useQueryAndPagination = <
1924
TSchema extends z.ZodTypeAny,
@@ -25,7 +30,7 @@ export const useQueryAndPagination = <
2530
options?: Partial<AbstractQueryOptions>,
2631
) =>
2732
useQueryAndPaginationVanilla(
28-
zodParser(schemaQuery),
33+
zodParser<TQuery, TSchema>(schemaQuery),
2934
defaultQuery,
3035
defaultPagination,
3136
options,

src/zod/routers/nextRouter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export const useQuery = <
1616
defaultQuery?: TDefaultQuery,
1717
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
1818
) =>
19-
useQueryVanilla<TQuery, TDefaultQuery>(
20-
zodParser(schemaQuery),
19+
useQueryVanilla(
20+
zodParser<TQuery, TSchema>(schemaQuery),
2121
defaultQuery,
2222
options,
2323
)
@@ -33,7 +33,7 @@ export const useQueryAndPagination = <
3333
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
3434
) =>
3535
useQueryAndPaginationVanilla(
36-
zodParser(schemaQuery),
36+
zodParser<TQuery, TSchema>(schemaQuery),
3737
defaultQuery,
3838
defaultPagination,
3939
options,

src/zod/routers/reactRouter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export const useQuery = <
1414
schemaQuery: TSchema,
1515
defaultQuery: Partial<TQuery> = {},
1616
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
17-
) => useQueryVanilla(zodParser(schemaQuery), defaultQuery, options)
17+
) =>
18+
useQueryVanilla(
19+
zodParser<TQuery, TSchema>(schemaQuery),
20+
defaultQuery,
21+
options,
22+
)
1823

1924
export const useQueryAndPagination = <
2025
TSchema extends z.ZodTypeAny,
@@ -26,7 +31,7 @@ export const useQueryAndPagination = <
2631
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
2732
) =>
2833
useQueryAndPaginationVanilla(
29-
zodParser(schemaQuery),
34+
zodParser<TQuery, TSchema>(schemaQuery),
3035
defaultQuery,
3136
defaultPagination,
3237
options,

0 commit comments

Comments
 (0)