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