Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

types.ts

506 lines18.2 KB

1/* istanbul ignore file */
2​
3import type {
4 DefaultError,
5 DefinedInfiniteQueryObserverResult,
6 DefinedQueryObserverResult,
7 DistributiveOmit,
8 InfiniteData,
9 InfiniteQueryExecuteOptions,
10 InfiniteQueryObserverOptions,
11 InfiniteQueryObserverResult,
12 MutateFunction,
13 MutationObserverOptions,
14 MutationObserverResult,
15 OmitKeyof,
16 Override,
17 QueryExecuteOptions,
18 QueryKey,
19 QueryObserverOptions,
20 QueryObserverResult,
21 SkipToken,
22} from '@tanstack/query-core'
23​
24/**
25 * {@link UseBaseQueryOptions} with all type parameters set to `any`, useful when the specific types aren't
26 * relevant, e.g. when accepting options for any query in a helper function.
27 */
28export type AnyUseBaseQueryOptions = UseBaseQueryOptions<
29 any,
30 any,
31 any,
32 any,
33 any
35/**
36 * The options shared by `useQuery` and `useSuspenseQuery`. Extends {@link QueryObserverOptions} from
37 * `@tanstack/query-core` with the `react-query`-specific `subscribed` option.
38 *
39 * @template TQueryFnData - The type your `queryFn` resolves to.
40 * @template TError - The type of errors your `queryFn` may throw.
41 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
42 * `select` is used.
43 * @template TQueryData - The type of the data actually held in the query cache β€” the input to `select` and
44 * `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
45 * @template TQueryKey - The type of your `queryKey`.
46 */
47export interface UseBaseQueryOptions<
48 TQueryFnData = unknown,
49 TError = DefaultError,
50 TData = TQueryFnData,
51 TQueryData = TQueryFnData,
52 TQueryKey extends QueryKey = QueryKey,
53> extends QueryObserverOptions<
54 TQueryFnData,
55 TError,
56 TData,
57 TQueryData,
58 TQueryKey
59> {
60 /**
61 * Set this to `false` to unsubscribe this observer from updates to the query cache.
62 *
63 * @defaultValue true
64 */
65 subscribed?: boolean
67​
68/**
69 * The options accepted by `usePrefetchQuery` β€” everything you can pass to `queryClient.query`, except `queryFn`
70 * is required unless a default query function has been defined.
71 *
72 * @template TQueryFnData - The type your `queryFn` resolves to.
73 * @template TError - The type of errors your `queryFn` may throw.
74 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
75 * `select` is used.
76 * @template TQueryData - The type of the data actually held in the query cache β€” the input to `select` and
77 * `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
78 * @template TQueryKey - The type of your `queryKey`.
79 */
80export type UsePrefetchQueryOptions<
81 TQueryFnData = unknown,
82 TError = DefaultError,
83 TData = TQueryFnData,
84 TQueryData = TQueryFnData,
85 TQueryKey extends QueryKey = QueryKey,
86> = DistributiveOmit<
87 QueryExecuteOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
88 'queryFn'
89> & {
90 /**
91 * `skipToken` is not allowed as a value here β€” a prefetch always needs a query function to actually run,
92 * unless a default query function has been defined.
93 */
94 queryFn?: Exclude<
95 QueryExecuteOptions<
96 TQueryFnData,
97 TError,
98 TData,
99 TQueryData,
100 TQueryKey
101 >['queryFn'],
102 SkipToken
103 >
105​
106/**
107 * The options accepted by `usePrefetchInfiniteQuery` β€” everything you can pass to `queryClient.infiniteQuery`,
108 * except `queryFn` is required unless a default query function has been defined.
109 *
110 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
111 * @template TError - The type of errors your `queryFn` may throw.
112 * @template TData - The type `data` ends up as after `select` runs. Defaults to `InfiniteData<TQueryFnData>`,
113 * the shape of all fetched pages plus their page params β€” a prefetch never reads `data` back out, so this
114 * parameter only matters if you reuse these options elsewhere with `select` applied.
115 * @template TQueryKey - The type of your `queryKey`.
116 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
117 */
118export type UsePrefetchInfiniteQueryOptions<
119 TQueryFnData = unknown,
120 TError = DefaultError,
121 TData = InfiniteData<TQueryFnData>,
122 TQueryKey extends QueryKey = QueryKey,
123 TPageParam = unknown,
124> = DistributiveOmit<
125 InfiniteQueryExecuteOptions<
126 TQueryFnData,
127 TError,
128 TData,
129 TQueryKey,
130 TPageParam
131 >,
132 'queryFn'
133> & {
134 /**
135 * `skipToken` is not allowed as a value here β€” a prefetch always needs a query function to actually run,
136 * unless a default query function has been defined.
137 */
138 queryFn?: Exclude<
139 InfiniteQueryExecuteOptions<
140 TQueryFnData,
141 TError,
142 TData,
143 TQueryKey,
144 TPageParam
145 >['queryFn'],
146 SkipToken
147 >
149​
150/**
151 * {@link UseQueryOptions} with all type parameters set to `any`, useful when the specific types aren't
152 * relevant, e.g. when accepting options for any query in a helper function.
153 */
154export type AnyUseQueryOptions = UseQueryOptions<any, any, any, any>
155/**
156 * The options accepted by `useQuery`. Same as {@link UseBaseQueryOptions}, minus `suspense` (which
157 * `react-query` derives from which hook you call rather than exposing as an option).
158 *
159 * @template TQueryFnData - The type your `queryFn` resolves to.
160 * @template TError - The type of errors your `queryFn` may throw.
161 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
162 * `select` is used.
163 * @template TQueryKey - The type of your `queryKey`.
164 */
165export interface UseQueryOptions<
166 TQueryFnData = unknown,
167 TError = DefaultError,
168 TData = TQueryFnData,
169 TQueryKey extends QueryKey = QueryKey,
170> extends OmitKeyof<
171 UseBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
172 'suspense'
173> {}
174​
175/**
176 * {@link UseSuspenseQueryOptions} with all type parameters set to `any`, useful when the specific types aren't
177 * relevant, e.g. when accepting options for any query in a helper function.
178 */
179export type AnyUseSuspenseQueryOptions = UseSuspenseQueryOptions<
180 any,
181 any,
182 any,
183 any
185/**
186 * The options accepted by `useSuspenseQuery`. Same as {@link UseQueryOptions}, minus `enabled`, `throwOnError`,
187 * and `placeholderData` β€” Suspense hooks cannot render a "disabled" or "placeholder" state, so those options
188 * don't apply.
189 *
190 * @template TQueryFnData - The type your `queryFn` resolves to.
191 * @template TError - The type of errors your `queryFn` may throw.
192 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
193 * `select` is used.
194 * @template TQueryKey - The type of your `queryKey`.
195 */
196export interface UseSuspenseQueryOptions<
197 TQueryFnData = unknown,
198 TError = DefaultError,
199 TData = TQueryFnData,
200 TQueryKey extends QueryKey = QueryKey,
201> extends OmitKeyof<
202 UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
203 'queryFn' | 'enabled' | 'throwOnError' | 'placeholderData'
204> {
205 /**
206 * `skipToken` is not allowed here β€” Suspense hooks cannot render a "disabled" state, so a query function
207 * must always be provided, unless a default query function has been defined.
208 */
209 queryFn?: Exclude<
210 UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],
211 SkipToken
212 >
214​
215/**
216 * {@link UseInfiniteQueryOptions} with all type parameters set to `any`, useful when the specific types aren't
217 * relevant, e.g. when accepting options for any query in a helper function.
218 */
219export type AnyUseInfiniteQueryOptions = UseInfiniteQueryOptions<
220 any,
221 any,
222 any,
223 any,
224 any
226/**
227 * The options accepted by `useInfiniteQuery`. Extends {@link InfiniteQueryObserverOptions} from
228 * `@tanstack/query-core` with the `react-query`-specific `subscribed` option, minus `suspense` (which
229 * `react-query` derives from which hook you call rather than exposing as an option).
230 *
231 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
232 * @template TError - The type of errors your `queryFn` may throw.
233 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
234 * the shape of all fetched pages plus their page params.
235 * @template TQueryKey - The type of your `queryKey`.
236 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
237 */
238export interface UseInfiniteQueryOptions<
239 TQueryFnData = unknown,
240 TError = DefaultError,
241 TData = InfiniteData<TQueryFnData>,
242 TQueryKey extends QueryKey = QueryKey,
243 TPageParam = unknown,
244> extends OmitKeyof<
245 InfiniteQueryObserverOptions<
246 TQueryFnData,
247 TError,
248 TData,
249 TQueryKey,
250 TPageParam
251 >,
252 'suspense'
253> {
254 /**
255 * Set this to `false` to unsubscribe this observer from updates to the query cache.
256 *
257 * @defaultValue true
258 */
259 subscribed?: boolean
261​
262/**
263 * {@link UseSuspenseInfiniteQueryOptions} with all type parameters set to `any`, useful when the specific types
264 * aren't relevant, e.g. when accepting options for any query in a helper function.
265 */
266export type AnyUseSuspenseInfiniteQueryOptions =
267 UseSuspenseInfiniteQueryOptions<any, any, any, any, any>
268/**
269 * The options accepted by `useSuspenseInfiniteQuery`. Same as {@link UseInfiniteQueryOptions}, minus `enabled`,
270 * `throwOnError`, and `placeholderData` β€” Suspense hooks cannot render a "disabled" or "placeholder" state, so
271 * those options don't apply.
272 *
273 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
274 * @template TError - The type of errors your `queryFn` may throw.
275 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
276 * the shape of all fetched pages plus their page params.
277 * @template TQueryKey - The type of your `queryKey`.
278 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
279 */
280export interface UseSuspenseInfiniteQueryOptions<
281 TQueryFnData = unknown,
282 TError = DefaultError,
283 TData = InfiniteData<TQueryFnData>,
284 TQueryKey extends QueryKey = QueryKey,
285 TPageParam = unknown,
286> extends OmitKeyof<
287 UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>,
288 'queryFn' | 'enabled' | 'throwOnError' | 'placeholderData'
289> {
290 /**
291 * `skipToken` is not allowed here β€” Suspense hooks cannot render a "disabled" state, so a query function
292 * must always be provided, unless a default query function has been defined.
293 */
294 queryFn?: Exclude<
295 UseInfiniteQueryOptions<
296 TQueryFnData,
297 TError,
298 TData,
299 TQueryKey,
300 TPageParam
301 >['queryFn'],
302 SkipToken
303 >
305​
306/**
307 * The result of `useQuery` when `initialData` isn't set β€” `data` may be `undefined` while the query is
308 * `pending`. Re-exports {@link QueryObserverResult} from `@tanstack/query-core`. `useInfiniteQuery` returns
309 * {@link UseInfiniteQueryResult} instead.
310 *
311 * @template TData - The type `data` ends up as after `select` runs.
312 * @template TError - The type of errors your `queryFn` may throw.
313 */
314export type UseBaseQueryResult<
315 TData = unknown,
316 TError = DefaultError,
317> = QueryObserverResult<TData, TError>
318​
319/**
320 * The result of `useQuery`. Same as {@link UseBaseQueryResult}.
321 *
322 * @template TData - The type `data` ends up as after `select` runs.
323 * @template TError - The type of errors your `queryFn` may throw.
324 */
325export type UseQueryResult<
326 TData = unknown,
327 TError = DefaultError,
328> = UseBaseQueryResult<TData, TError>
329​
330/**
331 * The result of `useSuspenseQuery`. Same as {@link DefinedUseQueryResult}, minus `isPlaceholderData` β€” always
332 * `false` on that type, so this drops the dead field rather than an active state.
333 *
334 * @template TData - The type `data` ends up as after `select` runs.
335 * @template TError - The type of errors your `queryFn` may throw.
336 */
337export type UseSuspenseQueryResult<
338 TData = unknown,
339 TError = DefaultError,
340> = DistributiveOmit<
341 DefinedQueryObserverResult<TData, TError>,
342 'isPlaceholderData'
344​
345/**
346 * The result of `useQuery` when `initialData` is set, or of `useSuspenseQuery` before the `isPlaceholderData`
347 * omission β€” `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
348 * Re-exports {@link DefinedQueryObserverResult} from `@tanstack/query-core`.
349 *
350 * @template TData - The type `data` ends up as after `select` runs.
351 * @template TError - The type of errors your `queryFn` may throw.
352 */
353export type DefinedUseQueryResult<
354 TData = unknown,
355 TError = DefaultError,
356> = DefinedQueryObserverResult<TData, TError>
357​
358/**
359 * The result of `useInfiniteQuery` when `initialData` isn't set β€” `data` may be `undefined` while the query is
360 * `pending`. Re-exports {@link InfiniteQueryObserverResult} from `@tanstack/query-core`.
361 *
362 * @template TData - The type `data` ends up as after `select` runs.
363 * @template TError - The type of errors your `queryFn` may throw.
364 */
365export type UseInfiniteQueryResult<
366 TData = unknown,
367 TError = DefaultError,
368> = InfiniteQueryObserverResult<TData, TError>
369​
370/**
371 * The result of `useInfiniteQuery` when `initialData` is set β€” `data` is never `undefined` (unless a
372 * `select` changes `TData` to include `undefined`). Re-exports {@link DefinedInfiniteQueryObserverResult}
373 * from `@tanstack/query-core`.
374 *
375 * @template TData - The type `data` ends up as after `select` runs.
376 * @template TError - The type of errors your `queryFn` may throw.
377 */
378export type DefinedUseInfiniteQueryResult<
379 TData = unknown,
380 TError = DefaultError,
381> = DefinedInfiniteQueryObserverResult<TData, TError>
382​
383/**
384 * The result of `useSuspenseInfiniteQuery`. Same as {@link DefinedUseInfiniteQueryResult}, minus
385 * `isPlaceholderData` β€” Suspense hooks never render placeholder data.
386 *
387 * @template TData - The type `data` ends up as after `select` runs.
388 * @template TError - The type of errors your `queryFn` may throw.
389 */
390export type UseSuspenseInfiniteQueryResult<
391 TData = unknown,
392 TError = DefaultError,
393> = OmitKeyof<
394 DefinedInfiniteQueryObserverResult<TData, TError>,
395 'isPlaceholderData'
397​
398/**
399 * {@link UseMutationOptions} with all type parameters set to `any`, useful when the specific types aren't
400 * relevant, e.g. when accepting options for any mutation in a helper function.
401 */
402export type AnyUseMutationOptions = UseMutationOptions<any, any, any, any>
403/**
404 * The options accepted by `useMutation`. Same as {@link MutationObserverOptions} from `@tanstack/query-core`,
405 * minus the internal `_defaulted` flag.
406 *
407 * @template TData - The type your mutation function resolves to.
408 * @template TError - The type of errors your mutation function may throw.
409 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
410 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
411 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
412 */
413export interface UseMutationOptions<
414 TData = unknown,
415 TError = DefaultError,
416 TVariables = void,
417 TOnMutateResult = unknown,
418> extends OmitKeyof<
419 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
420 '_defaulted'
421> {}
422​
423/**
424 * The type of `mutate`, as returned by `useMutation`. Forwards the variables (and an optional per-call
425 * `onSuccess`/`onError`/`onSettled`) to the underlying `mutate` call. Fire-and-forget β€” errors are surfaced
426 * through the mutation result, not thrown.
427 *
428 * @template TData - The type your mutation function resolves to.
429 * @template TError - The type of errors your mutation function may throw.
430 * @template TVariables - The type of the variable passed to `mutate`.
431 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
432 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
433 */
434export type UseMutateFunction<
435 TData = unknown,
436 TError = DefaultError,
437 TVariables = void,
438 TOnMutateResult = unknown,
439> = (
440 ...args: Parameters<
441 MutateFunction<TData, TError, TVariables, TOnMutateResult>
442 >
443) => void
444​
445/**
446 * The type of `mutateAsync`, as returned by `useMutation`. Similar to {@link UseMutateFunction}, but returns a
447 * promise which can be awaited.
448 *
449 * @template TData - The type your mutation function resolves to.
450 * @template TError - The type of errors your mutation function may throw.
451 * @template TVariables - The type of the variable passed to `mutateAsync`.
452 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
453 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
454 */
455export type UseMutateAsyncFunction<
456 TData = unknown,
457 TError = DefaultError,
458 TVariables = void,
459 TOnMutateResult = unknown,
460> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
461​
462/**
463 * The result of `useMutation`. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with
464 * `mutate` narrowed to the fire-and-forget {@link UseMutateFunction} signature, plus the added `mutateAsync`.
465 *
466 * @template TData - The type your mutation function resolves to.
467 * @template TError - The type of errors your mutation function may throw.
468 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
469 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
470 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
471 */
472export type UseBaseMutationResult<
473 TData = unknown,
474 TError = DefaultError,
475 TVariables = unknown,
476 TOnMutateResult = unknown,
477> = Override<
478 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
479 { mutate: UseMutateFunction<TData, TError, TVariables, TOnMutateResult> }
480> & {
481 /**
482 * Similar to `mutate`, but returns a promise which can be awaited.
483 */
484 mutateAsync: UseMutateAsyncFunction<
485 TData,
486 TError,
487 TVariables,
488 TOnMutateResult
489 >
491​
492/**
493 * The result of `useMutation`. Same as {@link UseBaseMutationResult}.
494 *
495 * @template TData - The type your mutation function resolves to.
496 * @template TError - The type of errors your mutation function may throw.
497 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
498 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
499 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
500 */
501export type UseMutationResult<
502 TData = unknown,
503 TError = DefaultError,
504 TVariables = unknown,
505 TOnMutateResult = unknown,
506> = UseBaseMutationResult<TData, TError, TVariables, TOnMutateResult>