Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { queryOptions } from '../queryOptions'
5import { useQuery } from '../useQuery'
6import { useQueries } from '../useQueries'
7import { useSuspenseQuery } from '../useSuspenseQuery'
8import type { AnyUseQueryOptions } from '../types'
9import type {
10 DataTag,
11 InitialDataFunction,
12 QueryPersister,
13} from '@tanstack/query-core'
14β
15// Regression test for exported queryOptions inference under declaration emit.
16// TypeScript should be able to name the return type without expanding the
17// internal data tag symbols into the consumer's .d.ts output.
18export const exportedQueryOptions = queryOptions({
19 queryKey: ['invalid'],
20})
21β
22describe('queryOptions', () => {
23 it('should not allow excess properties', () => {
24 assertType(
25 queryOptions({
26 queryKey: queryKey(),
27 queryFn: () => Promise.resolve(5),
28 // @ts-expect-error this is a good error, because stallTime does not exist!
29 stallTime: 1000,
30 }),
31 )
32 })
33 it('should infer types for callbacks', () => {
34 queryOptions({
35 queryKey: queryKey(),
36 queryFn: () => Promise.resolve(5),
37 staleTime: 1000,
38 select: (data) => {
39 expectTypeOf(data).toEqualTypeOf<number>()
40 },
41 })
42 })
43 it('should work when passed to useQuery', () => {
44 const options = queryOptions({
45 queryKey: queryKey(),
46 queryFn: () => Promise.resolve(5),
47 })
48β
49 const { data } = useQuery(options)
50 expectTypeOf(data).toEqualTypeOf<number | undefined>()
51 })
52 it('should work when passed to useSuspenseQuery', () => {
53 const options = queryOptions({
54 queryKey: queryKey(),
55 queryFn: () => Promise.resolve(5),
56 })
57β
58 const { data } = useSuspenseQuery(options)
59 expectTypeOf(data).toEqualTypeOf<number>()
60 })
61 it('should work when passed to query', async () => {
62 const options = queryOptions({
63 queryKey: ['key'],
64 queryFn: () => Promise.resolve(5),
65 })
66β
67 const data = await new QueryClient().query(options)
68 expectTypeOf(data).toEqualTypeOf<number>()
69 })
70β
71 it('should work when passed to fetchQuery', async () => {
72 const options = queryOptions({
73 queryKey: queryKey(),
74 queryFn: () => Promise.resolve(5),
75 })
76β
77 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
78 const data = await new QueryClient().fetchQuery(options)
79 expectTypeOf(data).toEqualTypeOf<number>()
80 })
81 it('should work when passed to query with select', async () => {
82 const options = queryOptions({
83 queryKey: ['key'],
84 queryFn: () => Promise.resolve(5),
85 select: (data) => data.toString(),
86 })
87β
88 const data = await new QueryClient().query(options)
89 expectTypeOf(data).toEqualTypeOf<string>()
90 })
91 it('should work when passed to query with enabled: false', async () => {
92 const options = queryOptions({
93 queryKey: ['key'],
94 queryFn: () => Promise.resolve(5),
95 enabled: false,
96 })
97β
98 const data = await new QueryClient().query(options)
99 expectTypeOf(data).toEqualTypeOf<number>()
100 })
101 it('should work when passed to query with skipToken', async () => {
102 const options = queryOptions({
103 queryKey: ['key'],
104 queryFn: skipToken,
105 })
106β
107 const data = await new QueryClient().query(options)
108 expectTypeOf(data).toEqualTypeOf<unknown>()
109 })
110 it('should ignore select when passed to fetchQuery', async () => {
111 const options = queryOptions({
112 queryKey: ['key'],
113 queryFn: () => Promise.resolve(5),
114 select: (data) => data.toString(),
115 })
116β
117 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
118 const data = await new QueryClient().fetchQuery(options)
119 expectTypeOf(data).toEqualTypeOf<number>()
120 })
121 it('should work when passed to useQueries', () => {
122 const options = queryOptions({
123 queryKey: queryKey(),
124 queryFn: () => Promise.resolve(5),
125 })
126β
127 const [{ data }] = useQueries({
128 queries: [options],
129 })
130β
131 expectTypeOf(data).toEqualTypeOf<number | undefined>()
132 })
133 it('should tag the queryKey with the result type of the QueryFn', () => {
134 const { queryKey: tagged } = queryOptions({
135 queryKey: queryKey(),
136 queryFn: () => Promise.resolve(5),
137 })
138β
139 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
140 })
141 it('should tag the queryKey even if no promise is returned', () => {
142 const { queryKey: tagged } = queryOptions({
143 queryKey: queryKey(),
144 queryFn: () => 5,
145 })
146β
147 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
148 })
149 it('should tag the queryKey with unknown if there is no queryFn', () => {
150 const { queryKey: tagged } = queryOptions({
151 queryKey: queryKey(),
152 })
153β
154 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
155 })
156 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
157 const { queryKey: tagged } = queryOptions({
158 queryKey: queryKey(),
159 queryFn: () => Promise.resolve(5),
160 select: (data) => data.toString(),
161 })
162β
163 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
164 })
165 it('should return the proper type when passed to getQueryData', () => {
166 const { queryKey: tagged } = queryOptions({
167 queryKey: queryKey(),
168 queryFn: () => Promise.resolve(5),
169 })
170β
171 const queryClient = new QueryClient()
172 const data = queryClient.getQueryData(tagged)
173 expectTypeOf(data).toEqualTypeOf<number | undefined>()
174 })
175 it('should return the proper type when passed to getQueryState', () => {
176 const { queryKey: tagged } = queryOptions({
177 queryKey: queryKey(),
178 queryFn: () => Promise.resolve(5),
179 })
180β
181 const queryClient = new QueryClient()
182 const state = queryClient.getQueryState(tagged)
183 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
184 })
185 it('should properly type updaterFn when passed to setQueryData', () => {
186 const { queryKey: tagged } = queryOptions({
187 queryKey: queryKey(),
188 queryFn: () => Promise.resolve(5),
189 })
190β
191 const queryClient = new QueryClient()
192 const data = queryClient.setQueryData(tagged, (prev) => {
193 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
194 return prev
195 })
196 expectTypeOf(data).toEqualTypeOf<number | undefined>()
197 })
198 it('should properly type value when passed to setQueryData', () => {
199 const { queryKey: tagged } = queryOptions({
200 queryKey: queryKey(),
201 queryFn: () => Promise.resolve(5),
202 })
203β
204 const queryClient = new QueryClient()
205β
206 // @ts-expect-error value should be a number
207 queryClient.setQueryData(tagged, '5')
208 // @ts-expect-error value should be a number
209 queryClient.setQueryData(tagged, () => '5')
210β
211 const data = queryClient.setQueryData(tagged, 5)
212 expectTypeOf(data).toEqualTypeOf<number | undefined>()
213 })
214β
215 it('should infer even if there is a conditional skipToken', () => {
216 const options = queryOptions({
217 queryKey: queryKey(),
218 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
219 })
220β
221 const queryClient = new QueryClient()
222 const data = queryClient.getQueryData(options.queryKey)
223 expectTypeOf(data).toEqualTypeOf<number | undefined>()
224 })
225β
226 it('should infer to unknown if we disable a query with just a skipToken', () => {
227 const options = queryOptions({
228 queryKey: queryKey(),
229 queryFn: skipToken,
230 })
231β
232 const queryClient = new QueryClient()
233 const data = queryClient.getQueryData(options.queryKey)
234 expectTypeOf(data).toEqualTypeOf<unknown>()
235 })
236β
237 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
238 const options = queryOptions({
239 queryKey: queryKey(),
240 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
241 })
242 // @ts-expect-error TS2345
243 const { data } = useSuspenseQuery(options)
244 expectTypeOf(data).toEqualTypeOf<number>()
245 })
246β
247 it('should allow undefined response in initialData', () => {
248 assertType((id: string | null) =>
249 queryOptions({
250 queryKey: ['todo', id],
251 queryFn: () =>
252 Promise.resolve({
253 id: '1',
254 title: 'Do Laundry',
255 }),
256 initialData: () =>
257 !id
258 ? undefined
259 : {
260 id,
261 title: 'Initial Data',
262 },
263 }),
264 )
265 })
266β
267 it('should allow optional initialData object', () => {
268 const testFn = (id?: string) => {
269 const options = queryOptions({
270 queryKey: queryKey(),
271 queryFn: () => Promise.resolve('something string'),
272 initialData: id ? 'initial string' : undefined,
273 })
274 expectTypeOf(options.initialData).toExtend<
275 InitialDataFunction<string> | string | undefined
276 >()
277 }
278 testFn('id')
279 testFn()
280 })
281β
282 it('should be passable to UseQueryOptions', () => {
283 function somethingWithQueryOptions<TQueryOpts extends AnyUseQueryOptions>(
284 options: TQueryOpts,
285 ) {
286 return options.queryKey
287 }
288β
289 const options = queryOptions({
290 queryKey: queryKey(),
291 queryFn: () => Promise.resolve(1),
292 })
293β
294 assertType(somethingWithQueryOptions(options))
295 })
296β
297 it('should return a custom query key type', () => {
298 type MyQueryKey = [Array<string>, { type: 'foo' }]
299β
300 const options = queryOptions({
301 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
302 queryFn: () => Promise.resolve(1),
303 })
304β
305 expectTypeOf(options.queryKey).toEqualTypeOf<
306 DataTag<MyQueryKey, number, Error>
307 >()
308 })
309β
310 it('should return a custom query key type with datatag', () => {
311 type MyQueryKey = DataTag<
312 [Array<string>, { type: 'foo' }],
313 number,
314 Error & { myMessage: string }
315 >
316β
317 const options = queryOptions({
318 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
319 queryFn: () => Promise.resolve(1),
320 })
321β
322 expectTypeOf(options.queryKey).toEqualTypeOf<
323 DataTag<MyQueryKey, number, Error & { myMessage: string }>
324 >()
325 })
326β
327 it('should infer TQueryFnData from persister paired with a queryFn declaring a parameter (#7842)', () => {
328 const persister = undefined as unknown as QueryPersister<string, any>
329β
330 const options = queryOptions({
331 queryKey: ['key'],
332 queryFn: (_context) => 'hello',
333 persister,
334 })
335β
336 expectTypeOf(options.queryFn!).returns.toEqualTypeOf<
337 string | Promise<string>
338 >()
339 })
340β
341 it('should still error when persister and queryFn return types genuinely conflict', () => {
342 const persister = undefined as unknown as QueryPersister<string, any>
343β
344 assertType(
345 queryOptions({
346 queryKey: ['key'],
347 // @ts-expect-error persister expects string, queryFn returns number
348 queryFn: () => 42,
349 persister,
350 }),
351 )
352β
353 assertType(
354 queryOptions({
355 queryKey: ['key'],
356 // @ts-expect-error persister expects string, queryFn with arg returns number
357 queryFn: (_context) => 42,
358 persister,
359 }),
360 )
361 })
362})