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

types.ts

490 lines15.5 KB

1/* istanbul ignore file */
2​
3import type {
4 DefaultError,
5 DefinedInfiniteQueryObserverResult,
6 DefinedQueryObserverResult,
7 InfiniteQueryObserverOptions,
8 InfiniteQueryObserverResult,
9 MutateFunction,
10 MutationObserverOptions,
11 MutationObserverResult,
12 OmitKeyof,
13 Override,
14 QueryKey,
15 QueryObserverOptions,
16 QueryObserverResult,
17} from '@tanstack/query-core'
18import type { Signal } from '@angular/core'
19import type { MapToSignals } from './utils/signal-proxy'
20import type {
21 InfiniteQueryResultFields,
22 MutationResultFields,
23 QueryResultFields,
24} from './utils/result-fields'
25​
26type SignalFunction<T extends () => any> = T & Signal<ReturnType<T>>
27​
28export type CreateQueryOptions<
29 TQueryFnData = unknown,
30 TError = DefaultError,
31 TData = TQueryFnData,
32 TQueryKey extends QueryKey = QueryKey,
33> = OmitKeyof<
34 QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
35 'notifyOnChangeProps' | 'suspense' | 'throwOnError'
37​
38type CreateStatusBasedQueryResult<
39 TStatus extends QueryObserverResult['status'],
40 TData = unknown,
41 TError = DefaultError,
42> = Extract<QueryObserverResult<TData, TError>, { status: TStatus }>
43​
44type CreateStatusBasedDefinedQueryResult<
45 TStatus extends DefinedQueryObserverResult['status'],
46 TData = unknown,
47 TError = DefaultError,
48> = Extract<DefinedQueryObserverResult<TData, TError>, { status: TStatus }>
49​
50type CreateStatusBasedInfiniteQueryResult<
51 TStatus extends InfiniteQueryObserverResult['status'],
52 TData = unknown,
53 TError = DefaultError,
54> = Extract<InfiniteQueryObserverResult<TData, TError>, { status: TStatus }>
55​
56type CreateStatusBasedDefinedInfiniteQueryResult<
57 TStatus extends DefinedInfiniteQueryObserverResult['status'],
58 TData = unknown,
59 TError = DefaultError,
60> = Extract<
61 DefinedInfiniteQueryObserverResult<TData, TError>,
62 { status: TStatus }
64​
65export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
66 isSuccess: SignalFunction<
67 (
68 this: CreateBaseQueryResult<TData, TError>,
69 ) => this is CreateBaseQueryResult<
70 TData,
71 TError,
72 CreateStatusBasedQueryResult<'success', TData, TError>
73 >
74 >
75 isError: SignalFunction<
76 (
77 this: CreateBaseQueryResult<TData, TError>,
78 ) => this is CreateBaseQueryResult<
79 TData,
80 TError,
81 CreateStatusBasedQueryResult<'error', TData, TError>
82 >
83 >
84 isPending: SignalFunction<
85 (
86 this: CreateBaseQueryResult<TData, TError>,
87 ) => this is CreateBaseQueryResult<
88 TData,
89 TError,
90 CreateStatusBasedQueryResult<'pending', TData, TError>
91 >
92 >
94​
95export interface DefinedQueryNarrowing<TData = unknown, TError = DefaultError> {
96 isSuccess: SignalFunction<
97 (
98 this: DefinedCreateQueryResult<TData, TError>,
99 ) => this is DefinedCreateQueryResult<
100 TData,
101 TError,
102 CreateStatusBasedDefinedQueryResult<'success', TData, TError>
103 >
104 >
105 isError: SignalFunction<
106 (
107 this: DefinedCreateQueryResult<TData, TError>,
108 ) => this is DefinedCreateQueryResult<
109 TData,
110 TError,
111 CreateStatusBasedDefinedQueryResult<'error', TData, TError>
112 >
113 >
114 isPending: SignalFunction<
115 (this: DefinedCreateQueryResult<TData, TError>) => this is never
116 >
118​
119export interface BaseInfiniteQueryNarrowing<
120 TData = unknown,
121 TError = DefaultError,
122> {
123 isSuccess: SignalFunction<
124 (
125 this: CreateInfiniteQueryResult<TData, TError>,
126 ) => this is CreateInfiniteQueryResult<
127 TData,
128 TError,
129 CreateStatusBasedInfiniteQueryResult<'success', TData, TError>
130 >
131 >
132 isError: SignalFunction<
133 (
134 this: CreateInfiniteQueryResult<TData, TError>,
135 ) => this is CreateInfiniteQueryResult<
136 TData,
137 TError,
138 CreateStatusBasedInfiniteQueryResult<'error', TData, TError>
139 >
140 >
141 isPending: SignalFunction<
142 (
143 this: CreateInfiniteQueryResult<TData, TError>,
144 ) => this is CreateInfiniteQueryResult<
145 TData,
146 TError,
147 CreateStatusBasedInfiniteQueryResult<'pending', TData, TError>
148 >
149 >
151​
152export interface DefinedInfiniteQueryNarrowing<
153 TData = unknown,
154 TError = DefaultError,
155> {
156 isSuccess: SignalFunction<
157 (
158 this: DefinedCreateInfiniteQueryResult<TData, TError>,
159 ) => this is DefinedCreateInfiniteQueryResult<
160 TData,
161 TError,
162 CreateStatusBasedDefinedInfiniteQueryResult<'success', TData, TError>
163 >
164 >
165 isError: SignalFunction<
166 (
167 this: DefinedCreateInfiniteQueryResult<TData, TError>,
168 ) => this is DefinedCreateInfiniteQueryResult<
169 TData,
170 TError,
171 CreateStatusBasedDefinedInfiniteQueryResult<'error', TData, TError>
172 >
173 >
174 isPending: SignalFunction<
175 (this: DefinedCreateInfiniteQueryResult<TData, TError>) => this is never
176 >
178​
179export interface CreateInfiniteQueryOptions<
180 TQueryFnData = unknown,
181 TError = DefaultError,
182 TData = TQueryFnData,
183 TQueryKey extends QueryKey = QueryKey,
184 TPageParam = unknown,
185> extends OmitKeyof<
186 InfiniteQueryObserverOptions<
187 TQueryFnData,
188 TError,
189 TData,
190 TQueryKey,
191 TPageParam
192 >,
193 'notifyOnChangeProps' | 'suspense' | 'throwOnError'
194> {}
195​
196/**
197 * The result of `injectQuery` when `initialData` isn't set β€” `data` may be `undefined` while the query is
198 * `pending`. Same shape as {@link QueryObserverResult} from `@tanstack/query-core`, but value fields (like
199 * `data`, `error`, `status`) are exposed as a `Signal` β€” read them with `query.data()`, not `query.data` β€”
200 * while function fields (like `refetch`) are called directly, unchanged. `isSuccess`/`isError`/`isPending`
201 * are {@link BaseQueryNarrowing} type-guard methods rather than plain booleans.
202 * `injectInfiniteQuery` returns {@link CreateInfiniteQueryResult} instead.
203 *
204 * @template TData - The type `data` ends up as after `select` runs.
205 * @template TError - The type of errors your `queryFn` may throw.
206 */
207export type CreateBaseQueryResult<
208 TData = unknown,
209 TError = DefaultError,
210 TState extends QueryObserverResult<TData, TError> = QueryObserverResult<
211 TData,
212 TError
213 >,
214> = BaseQueryNarrowing<TData, TError> &
215 MapToSignals<
216 OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>,
217 QueryResultFields
218 >
219​
220/**
221 * The result of `injectQuery`. Same as {@link CreateBaseQueryResult}.
222 *
223 * @template TData - The type `data` ends up as after `select` runs.
224 * @template TError - The type of errors your `queryFn` may throw.
225 */
226export type CreateQueryResult<
227 TData = unknown,
228 TError = DefaultError,
229> = CreateBaseQueryResult<TData, TError>
230​
231/**
232 * The result of `injectQuery` when `initialData` is set β€” `data` is never `undefined`. Same shape as
233 * {@link DefinedQueryObserverResult} from `@tanstack/query-core`, but value fields are exposed as a
234 * `Signal` while function fields are called directly, unchanged.
235 *
236 * @template TData - The type `data` ends up as after `select` runs.
237 * @template TError - The type of errors your `queryFn` may throw.
238 */
239export type DefinedCreateQueryResult<
240 TData = unknown,
241 TError = DefaultError,
242 TState extends DefinedQueryObserverResult<TData, TError> =
243 DefinedQueryObserverResult<TData, TError>,
244> = DefinedQueryNarrowing<TData, TError> &
245 MapToSignals<
246 OmitKeyof<TState, keyof DefinedQueryNarrowing, 'safely'>,
247 QueryResultFields
248 >
249​
250/**
251 * The result of `injectInfiniteQuery` when `initialData` isn't set β€” `data` may be `undefined` while the
252 * query is `pending`. Same shape as {@link InfiniteQueryObserverResult} from `@tanstack/query-core`, but
253 * value fields are exposed as a `Signal` while function fields (like `fetchNextPage`) are called directly,
254 * unchanged.
255 *
256 * @template TData - The type `data` ends up as after `select` runs.
257 * @template TError - The type of errors your `queryFn` may throw.
258 */
259export type CreateInfiniteQueryResult<
260 TData = unknown,
261 TError = DefaultError,
262 TState extends InfiniteQueryObserverResult<TData, TError> =
263 InfiniteQueryObserverResult<TData, TError>,
264> = BaseInfiniteQueryNarrowing<TData, TError> &
265 MapToSignals<TState, InfiniteQueryResultFields>
266​
267/**
268 * The result of `injectInfiniteQuery` when `initialData` is set β€” `data` is never `undefined`. Same shape as
269 * {@link DefinedInfiniteQueryObserverResult} from `@tanstack/query-core`, but value fields are exposed as a
270 * `Signal` while function fields are called directly, unchanged.
271 *
272 * @template TData - The type `data` ends up as after `select` runs.
273 * @template TError - The type of errors your `queryFn` may throw.
274 */
275export type DefinedCreateInfiniteQueryResult<
276 TData = unknown,
277 TError = DefaultError,
278 TDefinedInfiniteQueryObserver extends DefinedInfiniteQueryObserverResult<
279 TData,
280 TError
281 > = DefinedInfiniteQueryObserverResult<TData, TError>,
282> = DefinedInfiniteQueryNarrowing<TData, TError> &
283 MapToSignals<TDefinedInfiniteQueryObserver, InfiniteQueryResultFields>
284​
285/**
286 * The options accepted by `injectMutation`. Same as {@link MutationObserverOptions} from
287 * `@tanstack/query-core`, minus the internal `_defaulted` flag.
288 *
289 * @template TData - The type your mutation function resolves to.
290 * @template TError - The type of errors your mutation function may throw.
291 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
292 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
293 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
294 */
295export interface CreateMutationOptions<
296 TData = unknown,
297 TError = DefaultError,
298 TVariables = void,
299 TOnMutateResult = unknown,
300> extends OmitKeyof<
301 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
302 '_defaulted' | 'throwOnError'
303> {}
304​
305/**
306 * The type of `mutate`, as returned by `injectMutation`. Forwards the variables (and an optional per-call
307 * `onSuccess`/`onError`/`onSettled`) to the underlying `mutate` call. Fire-and-forget β€” errors are surfaced
308 * through the mutation result, not thrown.
309 *
310 * @template TData - The type your mutation function resolves to.
311 * @template TError - The type of errors your mutation function may throw.
312 * @template TVariables - The type of the variable passed to `mutate`.
313 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
314 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
315 */
316export type CreateMutateFunction<
317 TData = unknown,
318 TError = DefaultError,
319 TVariables = void,
320 TOnMutateResult = unknown,
321> = (
322 ...args: Parameters<
323 MutateFunction<TData, TError, TVariables, TOnMutateResult>
324 >
325) => void
326​
327/**
328 * The type of `mutateAsync`, as returned by `injectMutation`. Similar to {@link CreateMutateFunction}, but
329 * returns a promise which can be awaited.
330 *
331 * @template TData - The type your mutation function resolves to.
332 * @template TError - The type of errors your mutation function may throw.
333 * @template TVariables - The type of the variable passed to `mutateAsync`.
334 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
335 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
336 */
337export type CreateMutateAsyncFunction<
338 TData = unknown,
339 TError = DefaultError,
340 TVariables = void,
341 TOnMutateResult = unknown,
342> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
343​
344/**
345 * The pre-`Signal` shape {@link CreateMutationResult} is built from β€” not what `injectMutation` actually
346 * returns. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with `mutate` narrowed to the
347 * fire-and-forget {@link CreateMutateFunction} signature, plus the added `mutateAsync`.
348 *
349 * @template TData - The type your mutation function resolves to.
350 * @template TError - The type of errors your mutation function may throw.
351 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
352 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
353 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
354 */
355export type CreateBaseMutationResult<
356 TData = unknown,
357 TError = DefaultError,
358 TVariables = unknown,
359 TOnMutateResult = unknown,
360> = Override<
361 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
362 { mutate: CreateMutateFunction<TData, TError, TVariables, TOnMutateResult> }
363> & {
364 /**
365 * Similar to `mutate`, but returns a promise which can be awaited.
366 */
367 mutateAsync: CreateMutateAsyncFunction<
368 TData,
369 TError,
370 TVariables,
371 TOnMutateResult
372 >
374​
375type CreateStatusBasedMutationResult<
376 TStatus extends CreateBaseMutationResult['status'],
377 TData = unknown,
378 TError = DefaultError,
379 TVariables = unknown,
380 TOnMutateResult = unknown,
381> = Extract<
382 CreateBaseMutationResult<TData, TError, TVariables, TOnMutateResult>,
383 { status: TStatus }
385​
386export interface BaseMutationNarrowing<
387 TData = unknown,
388 TError = DefaultError,
389 TVariables = unknown,
390 TOnMutateResult = unknown,
391> {
392 isSuccess: SignalFunction<
393 (
394 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
395 ) => this is CreateMutationResult<
396 TData,
397 TError,
398 TVariables,
399 TOnMutateResult,
400 CreateStatusBasedMutationResult<
401 'success',
402 TData,
403 TError,
404 TVariables,
405 TOnMutateResult
406 >
407 >
408 >
409 isError: SignalFunction<
410 (
411 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
412 ) => this is CreateMutationResult<
413 TData,
414 TError,
415 TVariables,
416 TOnMutateResult,
417 CreateStatusBasedMutationResult<
418 'error',
419 TData,
420 TError,
421 TVariables,
422 TOnMutateResult
423 >
424 >
425 >
426 isPending: SignalFunction<
427 (
428 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
429 ) => this is CreateMutationResult<
430 TData,
431 TError,
432 TVariables,
433 TOnMutateResult,
434 CreateStatusBasedMutationResult<
435 'pending',
436 TData,
437 TError,
438 TVariables,
439 TOnMutateResult
440 >
441 >
442 >
443 isIdle: SignalFunction<
444 (
445 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
446 ) => this is CreateMutationResult<
447 TData,
448 TError,
449 TVariables,
450 TOnMutateResult,
451 CreateStatusBasedMutationResult<
452 'idle',
453 TData,
454 TError,
455 TVariables,
456 TOnMutateResult
457 >
458 >
459 >
461​
462/**
463 * The result of `injectMutation`. Based on {@link CreateBaseMutationResult}, but value fields are exposed as
464 * a `Signal` β€” read them with `mutation.data()`, not `mutation.data` β€” while function fields (`mutate`,
465 * `mutateAsync`, `reset`) are called directly, unchanged. `isSuccess`/`isError`/`isPending`/`isIdle` are
466 * {@link BaseMutationNarrowing} type-guard methods rather than plain booleans.
467 *
468 * @template TData - The type your mutation function resolves to.
469 * @template TError - The type of errors your mutation function may throw.
470 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
471 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
472 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
473 */
474export type CreateMutationResult<
475 TData = unknown,
476 TError = DefaultError,
477 TVariables = unknown,
478 TOnMutateResult = unknown,
479 TState = CreateStatusBasedMutationResult<
480 CreateBaseMutationResult['status'],
481 TData,
482 TError,
483 TVariables,
484 TOnMutateResult
485 >,
486> = BaseMutationNarrowing<TData, TError, TVariables, TOnMutateResult> &
487 MapToSignals<
488 OmitKeyof<TState, keyof BaseMutationNarrowing, 'safely'>,
489 MutationResultFields
490 >