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