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