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 query = useQuery(() => options)
50 expectTypeOf(query.data).toEqualTypeOf<number | undefined>()
51 })
52β
53 it('should work when passed to query', async () => {
54 const options = queryOptions({
55 queryKey: ['key'],
56 queryFn: () => Promise.resolve(5),
57 })
58β
59 const data = await new QueryClient().query(options)
60 expectTypeOf(data).toEqualTypeOf<number>()
61 })
62β
63 it('should work when passed to query with select', async () => {
64 const options = queryOptions({
65 queryKey: ['key'],
66 queryFn: () => Promise.resolve(5),
67 select: (data) => data.toString(),
68 })
69β
70 const data = await new QueryClient().query(options)
71 expectTypeOf(data).toEqualTypeOf<string>()
72 })
73β
74 it('should work when passed to query with enabled: false', async () => {
75 const options = queryOptions({
76 queryKey: ['key'],
77 queryFn: () => Promise.resolve(5),
78 enabled: false,
79 })
80β
81 const data = await new QueryClient().query(options)
82 expectTypeOf(data).toEqualTypeOf<number>()
83 })
84β
85 it('should work when passed to query with skipToken', async () => {
86 const options = queryOptions({
87 queryKey: ['key'],
88 queryFn: skipToken,
89 })
90β
91 const data = await new QueryClient().query(options)
92 expectTypeOf(data).toEqualTypeOf<unknown>()
93 })
94β
95 it('should work when passed to fetchQuery', async () => {
96 const options = queryOptions({
97 queryKey: createKey(),
98 queryFn: () => Promise.resolve(5),
99 })
100β
101 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
102 const data = await new QueryClient().fetchQuery(options)
103 expectTypeOf(data).toEqualTypeOf<number>()
104 })
105β
106 it('should tag the queryKey with the result type of the QueryFn', () => {
107 const { queryKey: tagged } = queryOptions({
108 queryKey: createKey(),
109 queryFn: () => Promise.resolve(5),
110 })
111β
112 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
113 })
114β
115 it('should keep data possibly undefined and tag the queryKey with the result type of the QueryFn when initialData may be undefined', () => {
116 const options = queryOptions({
117 queryKey: createKey(),
118 queryFn: () => Promise.resolve(5),
119 initialData: Math.random() > 0.5 ? 5 : undefined,
120 })
121 const query = useQuery(() => options)
122β
123 expectTypeOf(query.data).toEqualTypeOf<number | undefined>()
124 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<number>()
125 })
126β
127 it('should tag the queryKey even if no promise is returned', () => {
128 const { queryKey: tagged } = queryOptions({
129 queryKey: createKey(),
130 queryFn: () => 5,
131 })
132β
133 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
134 })
135β
136 it('should tag the queryKey with unknown if there is no queryFn', () => {
137 const { queryKey: tagged } = queryOptions({
138 queryKey: createKey(),
139 })
140β
141 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
142 })
143β
144 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
145 const { queryKey: tagged } = queryOptions({
146 queryKey: createKey(),
147 queryFn: () => Promise.resolve(5),
148 select: (data) => data.toString(),
149 })
150β
151 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
152 })
153β
154 it('should tag the queryKey with the default error type', () => {
155 const { queryKey: tagged } = queryOptions({
156 queryKey: createKey(),
157 queryFn: () => Promise.resolve(1),
158 })
159β
160 expectTypeOf(tagged[dataTagErrorSymbol]).toEqualTypeOf<Error>()
161 })
162β
163 it('should return the proper type when passed to getQueryData', () => {
164 const { queryKey: tagged } = queryOptions({
165 queryKey: createKey(),
166 queryFn: () => Promise.resolve(5),
167 })
168β
169 const queryClient = new QueryClient()
170 const data = queryClient.getQueryData(tagged)
171 expectTypeOf(data).toEqualTypeOf<number | undefined>()
172 })
173β
174 it('should return the proper type when passed to getQueryState', () => {
175 const { queryKey: tagged } = queryOptions({
176 queryKey: createKey(),
177 queryFn: () => Promise.resolve(5),
178 })
179β
180 const queryClient = new QueryClient()
181 const state = queryClient.getQueryState(tagged)
182 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
183 })
184β
185 it('should properly type updaterFn when passed to setQueryData', () => {
186 const { queryKey: tagged } = queryOptions({
187 queryKey: createKey(),
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β
199 it('should properly type value when passed to setQueryData', () => {
200 const { queryKey: tagged } = queryOptions({
201 queryKey: createKey(),
202 queryFn: () => Promise.resolve(5),
203 })
204β
205 const queryClient = new QueryClient()
206β
207 // @ts-expect-error value should be a number
208 queryClient.setQueryData(tagged, '5')
209 // @ts-expect-error value should be a number
210 queryClient.setQueryData(tagged, () => '5')
211β
212 const data = queryClient.setQueryData(tagged, 5)
213 expectTypeOf(data).toEqualTypeOf<number | undefined>()
214 })
215β
216 it('should infer even if there is a conditional skipToken', () => {
217 const options = queryOptions({
218 queryKey: createKey(),
219 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
220 })
221β
222 const queryClient = new QueryClient()
223 const data = queryClient.getQueryData(options.queryKey)
224 expectTypeOf(data).toEqualTypeOf<number | undefined>()
225 })
226β
227 it('should infer to unknown if we disable a query with just a skipToken', () => {
228 const options = queryOptions({
229 queryKey: createKey(),
230 queryFn: skipToken,
231 })
232β
233 const queryClient = new QueryClient()
234 const data = queryClient.getQueryData(options.queryKey)
235 expectTypeOf(data).toEqualTypeOf<unknown>()
236 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
237 })
238β
239 it('should allow optional initialData object', () => {
240 const options = queryOptions({
241 queryKey: createKey(),
242 queryFn: () => Promise.resolve('something string'),
243 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
244 })
245β
246 expectTypeOf(options.initialData).toExtend<
247 InitialDataFunction<string> | string | undefined
248 >()
249 })
250})