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