Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueryClient,
4 dataTagErrorSymbol,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { createKey } from '@tanstack/query-test-utils'
9import { useQuery } from '../useQuery'
10import { queryOptions } from '../queryOptions'
11import type { InitialDataFunction } from '@tanstack/query-core'
12β
13// Regression test for exported queryOptions inference under declaration emit.
14// TypeScript should be able to name the return type without expanding the
15// internal data tag symbols into the consumer's .d.ts output.
16export const exportedQueryOptions = queryOptions({
17 queryKey: ['invalid'],
18})
19β
20describe('queryOptions', () => {
21 it('should not allow excess properties', () => {
22 assertType(
23 queryOptions({
24 queryKey: createKey(),
25 queryFn: () => Promise.resolve(5),
26 // @ts-expect-error this is a good error, because stallTime does not exist!
27 stallTime: 1000,
28 }),
29 )
30 })
31β
32 it('should infer types for callbacks', () => {
33 queryOptions({
34 queryKey: createKey(),
35 queryFn: () => Promise.resolve(5),
36 staleTime: 1000,
37 select: (data) => {
38 expectTypeOf(data).toEqualTypeOf<number>()
39 },
40 })
41 })
42β
43 it('should work when passed to useQuery', () => {
44 const options = queryOptions({
45 queryKey: createKey(),
46 queryFn: () => Promise.resolve(5),
47 })
48β
49 const { data } = useQuery(() => options)
50 // Non-optional: `data` is a suspending read β the pre-fetch gap suspends
51 // into <Loading> instead of surfacing as `undefined`.
52 expectTypeOf(data).toEqualTypeOf<number>()
53 })
54β
55 it('should work when passed to query', async () => {
56 const options = queryOptions({
57 queryKey: ['key'],
58 queryFn: () => Promise.resolve(5),
59 })
60β
61 const data = await new QueryClient().query(options)
62 expectTypeOf(data).toEqualTypeOf<number>()
63 })
64β
65 it('should work when passed to query with select', async () => {
66 const options = queryOptions({
67 queryKey: ['key'],
68 queryFn: () => Promise.resolve(5),
69 select: (data) => data.toString(),
70 })
71β
72 const data = await new QueryClient().query(options)
73 expectTypeOf(data).toEqualTypeOf<string>()
74 })
75β
76 it('should work when passed to query with enabled: false', async () => {
77 const options = queryOptions({
78 queryKey: ['key'],
79 queryFn: () => Promise.resolve(5),
80 enabled: false,
81 })
82β
83 const data = await new QueryClient().query(options)
84 expectTypeOf(data).toEqualTypeOf<number>()
85 })
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: createKey(),
100 queryFn: () => Promise.resolve(5),
101 })
102β
103 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
104 const data = await new QueryClient().fetchQuery(options)
105 expectTypeOf(data).toEqualTypeOf<number>()
106 })
107β
108 it('should tag the queryKey with the result type of the QueryFn', () => {
109 const { queryKey: tagged } = queryOptions({
110 queryKey: createKey(),
111 queryFn: () => Promise.resolve(5),
112 })
113β
114 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
115 })
116β
117 it('should keep data defined and tag the queryKey with the result type of the QueryFn when initialData may be undefined', () => {
118 const options = queryOptions({
119 queryKey: createKey(),
120 queryFn: () => Promise.resolve(5),
121 initialData: Math.random() > 0.5 ? 5 : undefined,
122 })
123 const query = useQuery(() => options)
124β
125 expectTypeOf(query.data).toEqualTypeOf<number>()
126 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<number>()
127 })
128β
129 it('should tag the queryKey even if no promise is returned', () => {
130 const { queryKey: tagged } = queryOptions({
131 queryKey: createKey(),
132 queryFn: () => 5,
133 })
134β
135 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
136 })
137β
138 it('should tag the queryKey with unknown if there is no queryFn', () => {
139 const { queryKey: tagged } = queryOptions({
140 queryKey: createKey(),
141 })
142β
143 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
144 })
145β
146 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
147 const { queryKey: tagged } = queryOptions({
148 queryKey: createKey(),
149 queryFn: () => Promise.resolve(5),
150 select: (data) => data.toString(),
151 })
152β
153 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
154 })
155β
156 it('should tag the queryKey with the default error type', () => {
157 const { queryKey: tagged } = queryOptions({
158 queryKey: createKey(),
159 queryFn: () => Promise.resolve(1),
160 })
161β
162 expectTypeOf(tagged[dataTagErrorSymbol]).toEqualTypeOf<Error>()
163 })
164β
165 it('should return the proper type when passed to getQueryData', () => {
166 const { queryKey: tagged } = queryOptions({
167 queryKey: createKey(),
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β
176 it('should return the proper type when passed to getQueryState', () => {
177 const { queryKey: tagged } = queryOptions({
178 queryKey: createKey(),
179 queryFn: () => Promise.resolve(5),
180 })
181β
182 const queryClient = new QueryClient()
183 const state = queryClient.getQueryState(tagged)
184 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
185 })
186β
187 it('should properly type updaterFn when passed to setQueryData', () => {
188 const { queryKey: tagged } = queryOptions({
189 queryKey: createKey(),
190 queryFn: () => Promise.resolve(5),
191 })
192β
193 const queryClient = new QueryClient()
194 const data = queryClient.setQueryData(tagged, (prev) => {
195 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
196 return prev
197 })
198 expectTypeOf(data).toEqualTypeOf<number | undefined>()
199 })
200β
201 it('should properly type value when passed to setQueryData', () => {
202 const { queryKey: tagged } = queryOptions({
203 queryKey: createKey(),
204 queryFn: () => Promise.resolve(5),
205 })
206β
207 const queryClient = new QueryClient()
208β
209 // @ts-expect-error value should be a number
210 queryClient.setQueryData(tagged, '5')
211 // @ts-expect-error value should be a number
212 queryClient.setQueryData(tagged, () => '5')
213β
214 const data = queryClient.setQueryData(tagged, 5)
215 expectTypeOf(data).toEqualTypeOf<number | undefined>()
216 })
217β
218 it('should infer even if there is a conditional skipToken', () => {
219 const options = queryOptions({
220 queryKey: createKey(),
221 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
222 })
223β
224 const queryClient = new QueryClient()
225 const data = queryClient.getQueryData(options.queryKey)
226 expectTypeOf(data).toEqualTypeOf<number | undefined>()
227 })
228β
229 it('should infer to unknown if we disable a query with just a skipToken', () => {
230 const options = queryOptions({
231 queryKey: createKey(),
232 queryFn: skipToken,
233 })
234β
235 const queryClient = new QueryClient()
236 const data = queryClient.getQueryData(options.queryKey)
237 expectTypeOf(data).toEqualTypeOf<unknown>()
238 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
239 })
240β
241 it('should allow optional initialData object', () => {
242 const options = queryOptions({
243 queryKey: createKey(),
244 queryFn: () => Promise.resolve('something string'),
245 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
246 })
247β
248 expectTypeOf(options.initialData).toExtend<
249 InitialDataFunction<string> | string | undefined
250 >()
251 })
252})