Skip to content

Commit 695d367

Browse files
authored
Avoid rest destructuring React Query result (#1925)
use TS hack to avoid bad ...rest destructure of RQ result
1 parent 3007029 commit 695d367

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

libs/api/hooks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type UseMutationOptions,
1919
type UseQueryOptions,
2020
} from '@tanstack/react-query'
21+
import { type SetNonNullable } from 'type-fest'
2122

2223
import { invariant } from '@oxide/util'
2324

@@ -138,7 +139,7 @@ export const getUsePrefetchedApiQuery =
138139
options: UseQueryOtherOptions<Result<A[M]>, ApiError> = {}
139140
) => {
140141
const queryKey = [method, params]
141-
const { data, ...rest } = useQuery({
142+
const result = useQuery({
142143
queryKey,
143144
// no catch, let unexpected errors bubble up
144145
queryFn: ({ signal }) => api[method](params, { signal }).then(handleResult(method)),
@@ -149,8 +150,13 @@ export const getUsePrefetchedApiQuery =
149150
throwOnError: (err) => err.statusCode === 404,
150151
...options,
151152
})
152-
invariant(data, `Expected query to be prefetched. Key: ${JSON.stringify(queryKey)}`)
153-
return { data, ...rest }
153+
invariant(
154+
result.data,
155+
`Expected query to be prefetched. Key: ${JSON.stringify(queryKey)}`
156+
)
157+
// TS infers non-nullable on a freestanding variable, but doesn't like to do
158+
// it on a property. So we give it a hint
159+
return result as SetNonNullable<typeof result, 'data'>
154160
}
155161

156162
const ERRORS_ALLOWED = 'errors-allowed'

0 commit comments

Comments
 (0)