@tanstack/angular-query-experimental 5.103.1 → 5.103.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/dist/create-base-query.d.mts +169 -0
  2. package/dist/create-base-query.d.mts.map +1 -0
  3. package/dist/create-base-query.mjs +91 -0
  4. package/dist/create-base-query.mjs.map +1 -0
  5. package/dist/devtools/index.d.mts +3 -0
  6. package/dist/devtools/index.mjs +2 -0
  7. package/dist/devtools/production/index.d.mts +4 -0
  8. package/dist/devtools/production/index.mjs +2 -0
  9. package/dist/devtools/stub.d.mts +6 -0
  10. package/dist/devtools/stub.d.mts.map +1 -0
  11. package/dist/devtools/stub.mjs +9 -0
  12. package/dist/devtools/stub.mjs.map +1 -0
  13. package/dist/devtools/types.d.mts +107 -0
  14. package/dist/devtools/types.d.mts.map +1 -0
  15. package/dist/devtools/types.mjs +1 -0
  16. package/{devtools/with-devtools.d.ts → dist/devtools/with-devtools.d.mts} +6 -2
  17. package/dist/devtools/with-devtools.d.mts.map +1 -0
  18. package/dist/devtools/with-devtools.mjs +110 -0
  19. package/dist/devtools/with-devtools.mjs.map +1 -0
  20. package/dist/devtools-panel/index.d.mts +3 -0
  21. package/dist/devtools-panel/index.mjs +2 -0
  22. package/{devtools-panel/inject-devtools-panel.d.ts → dist/devtools-panel/inject-devtools-panel.d.mts} +6 -2
  23. package/dist/devtools-panel/inject-devtools-panel.d.mts.map +1 -0
  24. package/dist/devtools-panel/inject-devtools-panel.mjs +67 -0
  25. package/dist/devtools-panel/inject-devtools-panel.mjs.map +1 -0
  26. package/dist/devtools-panel/production/index.d.mts +4 -0
  27. package/dist/devtools-panel/production/index.mjs +2 -0
  28. package/dist/devtools-panel/stub.d.mts +6 -0
  29. package/dist/devtools-panel/stub.d.mts.map +1 -0
  30. package/dist/devtools-panel/stub.mjs +7 -0
  31. package/dist/devtools-panel/stub.mjs.map +1 -0
  32. package/dist/devtools-panel/types.d.mts +52 -0
  33. package/dist/devtools-panel/types.d.mts.map +1 -0
  34. package/dist/devtools-panel/types.mjs +1 -0
  35. package/dist/index.d.mts +16 -0
  36. package/{index.mjs → dist/index.mjs} +8 -21
  37. package/dist/index.mjs.map +1 -0
  38. package/{infinite-query-options.d.ts → dist/infinite-query-options.d.mts} +35 -31
  39. package/dist/infinite-query-options.d.mts.map +1 -0
  40. package/dist/infinite-query-options.mjs +8 -0
  41. package/dist/infinite-query-options.mjs.map +1 -0
  42. package/{inject-infinite-query.d.ts → dist/inject-infinite-query.d.mts} +18 -14
  43. package/dist/inject-infinite-query.d.mts.map +1 -0
  44. package/dist/inject-infinite-query.mjs +13 -0
  45. package/dist/inject-infinite-query.mjs.map +1 -0
  46. package/{inject-is-fetching.d.ts → dist/inject-is-fetching.d.mts} +14 -10
  47. package/dist/inject-is-fetching.d.mts.map +1 -0
  48. package/dist/inject-is-fetching.mjs +69 -0
  49. package/dist/inject-is-fetching.mjs.map +1 -0
  50. package/{inject-is-mutating.d.ts → dist/inject-is-mutating.d.mts} +14 -10
  51. package/dist/inject-is-mutating.d.mts.map +1 -0
  52. package/dist/inject-is-mutating.mjs +52 -0
  53. package/dist/inject-is-mutating.mjs.map +1 -0
  54. package/{inject-is-restoring.d.ts → dist/inject-is-restoring.d.mts} +13 -10
  55. package/dist/inject-is-restoring.d.mts.map +1 -0
  56. package/dist/inject-is-restoring.mjs +35 -0
  57. package/dist/inject-is-restoring.mjs.map +1 -0
  58. package/{inject-mutation-state.d.ts → dist/inject-mutation-state.d.mts} +16 -13
  59. package/dist/inject-mutation-state.d.mts.map +1 -0
  60. package/dist/inject-mutation-state.mjs +103 -0
  61. package/dist/inject-mutation-state.mjs.map +1 -0
  62. package/{inject-mutation.d.ts → dist/inject-mutation.d.mts} +15 -11
  63. package/dist/inject-mutation.d.mts.map +1 -0
  64. package/dist/inject-mutation.mjs +226 -0
  65. package/dist/inject-mutation.mjs.map +1 -0
  66. package/dist/inject-queries-experimental/index.d.mts +2 -0
  67. package/dist/inject-queries-experimental/index.mjs +2 -0
  68. package/{inject-queries.d.ts → dist/inject-queries.d.mts} +36 -55
  69. package/dist/inject-queries.d.mts.map +1 -0
  70. package/dist/inject-queries.mjs +203 -0
  71. package/dist/inject-queries.mjs.map +1 -0
  72. package/{inject-query-client.d.ts → dist/inject-query-client.d.mts} +8 -4
  73. package/dist/inject-query-client.d.mts.map +1 -0
  74. package/dist/inject-query-client.mjs +23 -0
  75. package/dist/inject-query-client.mjs.map +1 -0
  76. package/{inject-query.d.ts → dist/inject-query.d.mts} +18 -14
  77. package/dist/inject-query.d.mts.map +1 -0
  78. package/dist/inject-query.mjs +12 -0
  79. package/dist/inject-query.mjs.map +1 -0
  80. package/{mutation-options.d.ts → dist/mutation-options.d.mts} +8 -4
  81. package/dist/mutation-options.d.mts.map +1 -0
  82. package/dist/mutation-options.mjs +8 -0
  83. package/dist/mutation-options.mjs.map +1 -0
  84. package/dist/pending-tasks-compat.d.mts +10 -0
  85. package/dist/pending-tasks-compat.d.mts.map +1 -0
  86. package/dist/pending-tasks-compat.mjs +13 -0
  87. package/dist/pending-tasks-compat.mjs.map +1 -0
  88. package/{providers.d.ts → dist/providers.d.mts} +16 -13
  89. package/dist/providers.d.mts.map +1 -0
  90. package/dist/providers.mjs +124 -0
  91. package/dist/providers.mjs.map +1 -0
  92. package/{query-options.d.ts → dist/query-options.d.mts} +41 -37
  93. package/dist/query-options.d.mts.map +1 -0
  94. package/dist/query-options.mjs +8 -0
  95. package/dist/query-options.mjs.map +1 -0
  96. package/dist/signal-proxy.d.mts +13 -0
  97. package/dist/signal-proxy.d.mts.map +1 -0
  98. package/dist/signal-proxy.mjs +35 -0
  99. package/dist/signal-proxy.mjs.map +1 -0
  100. package/{types.d.ts → dist/types.d.mts} +36 -37
  101. package/dist/types.d.mts.map +1 -0
  102. package/dist/types.mjs +6 -0
  103. package/dist/types.mjs.map +1 -0
  104. package/package.json +48 -20
  105. package/src/__tests__/infinite-query-options.test-d.ts +314 -0
  106. package/src/__tests__/infinite-query-options.test.ts +19 -0
  107. package/src/__tests__/inject-devtools-panel.test.ts +196 -0
  108. package/src/__tests__/inject-infinite-query.test-d.ts +66 -0
  109. package/src/__tests__/inject-infinite-query.test.ts +227 -0
  110. package/src/__tests__/inject-is-fetching.test.ts +108 -0
  111. package/src/__tests__/inject-is-mutating.test.ts +113 -0
  112. package/src/__tests__/inject-is-restoring.test.ts +93 -0
  113. package/src/__tests__/inject-mutation-state.test-d.ts +22 -0
  114. package/src/__tests__/inject-mutation-state.test.ts +198 -0
  115. package/src/__tests__/inject-mutation.test-d.ts +158 -0
  116. package/src/__tests__/inject-mutation.test.ts +1161 -0
  117. package/src/__tests__/inject-queries.test-d.ts +196 -0
  118. package/src/__tests__/inject-queries.test.ts +240 -0
  119. package/src/__tests__/inject-query.test-d.ts +357 -0
  120. package/src/__tests__/inject-query.test.ts +700 -0
  121. package/src/__tests__/mutation-options.test-d.ts +234 -0
  122. package/src/__tests__/mutation-options.test.ts +344 -0
  123. package/src/__tests__/pending-tasks.test.ts +644 -0
  124. package/src/__tests__/provide-query-client.test.ts +38 -0
  125. package/src/__tests__/provide-tanstack-query.test.ts +38 -0
  126. package/src/__tests__/query-options.test-d.ts +262 -0
  127. package/src/__tests__/query-options.test.ts +14 -0
  128. package/src/__tests__/signal-proxy.test.ts +27 -0
  129. package/src/__tests__/test-utils.ts +77 -0
  130. package/src/__tests__/with-devtools.test.ts +674 -0
  131. package/src/create-base-query.ts +175 -0
  132. package/src/devtools/index.ts +8 -0
  133. package/src/devtools/production/index.ts +1 -0
  134. package/src/devtools/stub.ts +7 -0
  135. package/src/devtools/types.ts +116 -0
  136. package/src/devtools/with-devtools.ts +182 -0
  137. package/src/devtools-panel/index.ts +8 -0
  138. package/src/devtools-panel/inject-devtools-panel.ts +110 -0
  139. package/src/devtools-panel/production/index.ts +1 -0
  140. package/src/devtools-panel/stub.ts +7 -0
  141. package/src/devtools-panel/types.ts +57 -0
  142. package/src/index.ts +60 -0
  143. package/src/infinite-query-options.ts +354 -0
  144. package/src/inject-infinite-query.ts +296 -0
  145. package/src/inject-is-fetching.ts +97 -0
  146. package/src/inject-is-mutating.ts +80 -0
  147. package/src/inject-is-restoring.ts +53 -0
  148. package/src/inject-mutation-state.ts +167 -0
  149. package/src/inject-mutation.ts +328 -0
  150. package/src/inject-queries-experimental/index.ts +1 -0
  151. package/src/inject-queries.ts +479 -0
  152. package/src/inject-query-client.ts +22 -0
  153. package/src/inject-query.ts +208 -0
  154. package/src/mutation-options.ts +121 -0
  155. package/src/pending-tasks-compat.ts +27 -0
  156. package/src/providers.ts +170 -0
  157. package/src/query-options.ts +292 -0
  158. package/src/signal-proxy.ts +46 -0
  159. package/src/types.ts +429 -0
  160. package/create-base-query.d.ts +0 -164
  161. package/create-base-query.mjs +0 -99
  162. package/create-base-query.mjs.map +0 -1
  163. package/devtools/index.d.ts +0 -2
  164. package/devtools/index.mjs +0 -5
  165. package/devtools/index.mjs.map +0 -1
  166. package/devtools/production/index.d.ts +0 -1
  167. package/devtools/stub.d.ts +0 -2
  168. package/devtools/stub.mjs +0 -8
  169. package/devtools/stub.mjs.map +0 -1
  170. package/devtools/types.d.ts +0 -100
  171. package/devtools/with-devtools.mjs +0 -106
  172. package/devtools/with-devtools.mjs.map +0 -1
  173. package/devtools-panel/index.d.ts +0 -2
  174. package/devtools-panel/index.mjs +0 -5
  175. package/devtools-panel/index.mjs.map +0 -1
  176. package/devtools-panel/inject-devtools-panel.mjs +0 -72
  177. package/devtools-panel/inject-devtools-panel.mjs.map +0 -1
  178. package/devtools-panel/production/index.d.ts +0 -1
  179. package/devtools-panel/stub.d.ts +0 -2
  180. package/devtools-panel/stub.mjs +0 -8
  181. package/devtools-panel/stub.mjs.map +0 -1
  182. package/devtools-panel/types.d.ts +0 -48
  183. package/index.d.ts +0 -25
  184. package/index.mjs.map +0 -1
  185. package/infinite-query-options.mjs +0 -7
  186. package/infinite-query-options.mjs.map +0 -1
  187. package/inject-infinite-query.mjs +0 -18
  188. package/inject-infinite-query.mjs.map +0 -1
  189. package/inject-is-fetching.mjs +0 -31
  190. package/inject-is-fetching.mjs.map +0 -1
  191. package/inject-is-mutating.mjs +0 -31
  192. package/inject-is-mutating.mjs.map +0 -1
  193. package/inject-is-restoring.mjs +0 -21
  194. package/inject-is-restoring.mjs.map +0 -1
  195. package/inject-mutation-state.mjs +0 -51
  196. package/inject-mutation-state.mjs.map +0 -1
  197. package/inject-mutation.mjs +0 -94
  198. package/inject-mutation.mjs.map +0 -1
  199. package/inject-queries-experimental/index.d.ts +0 -1
  200. package/inject-queries-experimental/index.mjs +0 -5
  201. package/inject-queries-experimental/index.mjs.map +0 -1
  202. package/inject-queries.mjs +0 -82
  203. package/inject-queries.mjs.map +0 -1
  204. package/inject-query-client.mjs +0 -9
  205. package/inject-query-client.mjs.map +0 -1
  206. package/inject-query.mjs +0 -14
  207. package/inject-query.mjs.map +0 -1
  208. package/mutation-options.mjs +0 -7
  209. package/mutation-options.mjs.map +0 -1
  210. package/pending-tasks-compat.d.ts +0 -7
  211. package/pending-tasks-compat.mjs +0 -19
  212. package/pending-tasks-compat.mjs.map +0 -1
  213. package/providers.mjs +0 -32
  214. package/providers.mjs.map +0 -1
  215. package/query-options.mjs +0 -7
  216. package/query-options.mjs.map +0 -1
  217. package/signal-proxy.d.ts +0 -11
  218. package/signal-proxy.mjs +0 -29
  219. package/signal-proxy.mjs.map +0 -1
@@ -0,0 +1,357 @@
1
+ import { describe, expectTypeOf, it } from 'vitest'
2
+ import { queryKey, sleep } from '@tanstack/query-test-utils'
3
+ import { injectQuery, queryOptions } from '..'
4
+ import type { Signal } from '@angular/core'
5
+ import type { CreateQueryOptions, OmitKeyof, QueryFunction } from '..'
6
+
7
+ describe('injectQuery', () => {
8
+ it('should return the correct types', () => {
9
+ const key = queryKey()
10
+ // unspecified query function should default to unknown
11
+ const noQueryFn = injectQuery(() => ({
12
+ queryKey: key,
13
+ }))
14
+ expectTypeOf(noQueryFn.data()).toEqualTypeOf<unknown>()
15
+ expectTypeOf(noQueryFn.error()).toEqualTypeOf<Error | null>()
16
+
17
+ // it should infer the result type from the query function
18
+ const fromQueryFn = injectQuery(() => ({
19
+ queryKey: key,
20
+ queryFn: () => 'test',
21
+ }))
22
+ expectTypeOf(fromQueryFn.data()).toEqualTypeOf<string | undefined>()
23
+ expectTypeOf(fromQueryFn.error()).toEqualTypeOf<Error | null>()
24
+
25
+ // it should be possible to specify the result type
26
+ const withResult = injectQuery<string>(() => ({
27
+ queryKey: key,
28
+ queryFn: () => 'test',
29
+ }))
30
+ expectTypeOf(withResult.data()).toEqualTypeOf<string | undefined>()
31
+ expectTypeOf(withResult.error()).toEqualTypeOf<Error | null>()
32
+
33
+ // it should be possible to specify the error type
34
+ type CustomErrorType = { message: string }
35
+ const withError = injectQuery<string, CustomErrorType>(() => ({
36
+ queryKey: key,
37
+ queryFn: () => 'test',
38
+ }))
39
+ expectTypeOf(withError.data()).toEqualTypeOf<string | undefined>()
40
+ expectTypeOf(withError.error()).toEqualTypeOf<CustomErrorType | null>()
41
+
42
+ // it should infer the result type from the configuration
43
+ const withResultInfer = injectQuery(() => ({
44
+ queryKey: key,
45
+ queryFn: () => true,
46
+ }))
47
+ expectTypeOf(withResultInfer.data()).toEqualTypeOf<boolean | undefined>()
48
+ expectTypeOf(withResultInfer.error()).toEqualTypeOf<Error | null>()
49
+
50
+ // it should be possible to specify a union type as result type
51
+ const unionTypeSync = injectQuery(() => ({
52
+ queryKey: key,
53
+ queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)),
54
+ }))
55
+ expectTypeOf(unionTypeSync.data()).toEqualTypeOf<'a' | 'b' | undefined>()
56
+ const unionTypeAsync = injectQuery<'a' | 'b'>(() => ({
57
+ queryKey: key,
58
+ queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
59
+ }))
60
+ expectTypeOf(unionTypeAsync.data()).toEqualTypeOf<'a' | 'b' | undefined>()
61
+
62
+ // it should error when the query function result does not match with the specified type
63
+ // @ts-expect-error
64
+ injectQuery<number>(() => ({ queryKey: key, queryFn: () => 'test' }))
65
+
66
+ // it should infer the result type from a generic query function
67
+ /**
68
+ *
69
+ */
70
+ function queryFn<T = string>(): Promise<T> {
71
+ return Promise.resolve({} as T)
72
+ }
73
+
74
+ const fromGenericQueryFn = injectQuery(() => ({
75
+ queryKey: key,
76
+ queryFn: () => queryFn(),
77
+ }))
78
+ expectTypeOf(fromGenericQueryFn.data()).toEqualTypeOf<string | undefined>()
79
+ expectTypeOf(fromGenericQueryFn.error()).toEqualTypeOf<Error | null>()
80
+
81
+ type MyData = number
82
+ type MyQueryKey = readonly ['my-data', number]
83
+
84
+ const getMyDataArrayKey: QueryFunction<MyData, MyQueryKey> = ({
85
+ queryKey: [, n],
86
+ }) => {
87
+ return n + 42
88
+ }
89
+
90
+ const fromMyDataArrayKeyQueryFn = injectQuery(() => ({
91
+ queryKey: ['my-data', 100] as const,
92
+ queryFn: getMyDataArrayKey,
93
+ }))
94
+ expectTypeOf(fromMyDataArrayKeyQueryFn.data()).toEqualTypeOf<
95
+ number | undefined
96
+ >()
97
+
98
+ // it should handle query-functions that return Promise<any>
99
+ const fromPromiseAnyQueryFn = injectQuery(() => ({
100
+ queryKey: key,
101
+ queryFn: () => fetch('return Promise<any>').then((resp) => resp.json()),
102
+ }))
103
+ expectTypeOf(fromPromiseAnyQueryFn.data()).toEqualTypeOf<any | undefined>()
104
+
105
+ const getMyDataStringKey: QueryFunction<MyData, ['1']> = (context) => {
106
+ expectTypeOf(context.queryKey).toEqualTypeOf<['1']>()
107
+ return Number(context.queryKey[0]) + 42
108
+ }
109
+
110
+ const fromGetMyDataStringKeyQueryFn = injectQuery(() => ({
111
+ queryKey: ['1'] as ['1'],
112
+ queryFn: getMyDataStringKey,
113
+ }))
114
+ expectTypeOf(fromGetMyDataStringKeyQueryFn.data()).toEqualTypeOf<
115
+ number | undefined
116
+ >()
117
+
118
+ // handles wrapped queries with custom fetcher passed as inline queryFn
119
+ const createWrappedQuery = <
120
+ TQueryKey extends [string, Record<string, unknown>?],
121
+ TQueryFnData,
122
+ TError,
123
+ TData = TQueryFnData,
124
+ >(
125
+ qk: TQueryKey,
126
+ fetcher: (
127
+ obj: TQueryKey[1],
128
+ token: string,
129
+ // return type must be wrapped with TQueryFnReturn
130
+ ) => Promise<TQueryFnData>,
131
+ options?: OmitKeyof<
132
+ CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
133
+ 'queryKey' | 'queryFn' | 'initialData',
134
+ 'safely'
135
+ >,
136
+ ) =>
137
+ injectQuery(() => ({
138
+ queryKey: qk,
139
+ queryFn: () => fetcher(qk[1], 'token'),
140
+ ...options,
141
+ }))
142
+ const fromWrappedQuery = createWrappedQuery([''], () =>
143
+ Promise.resolve('1'),
144
+ )
145
+ expectTypeOf(fromWrappedQuery.data()).toEqualTypeOf<string | undefined>()
146
+
147
+ // handles wrapped queries with custom fetcher passed directly to createQuery
148
+ const createWrappedFuncStyleQuery = <
149
+ TQueryKey extends [string, Record<string, unknown>?],
150
+ TQueryFnData,
151
+ TError,
152
+ TData = TQueryFnData,
153
+ >(
154
+ qk: TQueryKey,
155
+ fetcher: () => Promise<TQueryFnData>,
156
+ options?: OmitKeyof<
157
+ CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
158
+ 'queryKey' | 'queryFn' | 'initialData',
159
+ 'safely'
160
+ >,
161
+ ) => injectQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options }))
162
+ const fromWrappedFuncStyleQuery = createWrappedFuncStyleQuery([''], () =>
163
+ Promise.resolve(true),
164
+ )
165
+ expectTypeOf(fromWrappedFuncStyleQuery.data()).toEqualTypeOf<
166
+ boolean | undefined
167
+ >()
168
+ })
169
+
170
+ describe('initialData', () => {
171
+ describe('Config object overload', () => {
172
+ it('TData should always be defined when initialData is provided as an object', () => {
173
+ const key = queryKey()
174
+ const { data } = injectQuery(() => ({
175
+ queryKey: key,
176
+ queryFn: () => ({ wow: true }),
177
+ initialData: { wow: true },
178
+ }))
179
+
180
+ expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
181
+ })
182
+
183
+ it('TData should be defined when passed through queryOptions', () => {
184
+ const key = queryKey()
185
+ const options = () =>
186
+ queryOptions({
187
+ queryKey: key,
188
+ queryFn: () => {
189
+ return {
190
+ wow: true,
191
+ }
192
+ },
193
+ initialData: {
194
+ wow: true,
195
+ },
196
+ })
197
+ const { data } = injectQuery(options)
198
+
199
+ expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
200
+ })
201
+
202
+ it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
203
+ const key = queryKey()
204
+ const options = queryOptions({
205
+ queryKey: key,
206
+ queryFn: () => Promise.resolve(1),
207
+ })
208
+
209
+ const query = injectQuery(() => ({
210
+ ...options,
211
+ select: (data) => data > 1,
212
+ }))
213
+
214
+ expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
215
+ })
216
+
217
+ it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
218
+ const key = queryKey()
219
+ const { data } = injectQuery(() => ({
220
+ queryKey: key,
221
+ queryFn: () => {
222
+ return {
223
+ wow: true,
224
+ }
225
+ },
226
+ initialData: () => ({
227
+ wow: true,
228
+ }),
229
+ }))
230
+
231
+ expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
232
+ })
233
+
234
+ it('TData should have undefined in the union when initialData is NOT provided', () => {
235
+ const key = queryKey()
236
+ const { data } = injectQuery(() => ({
237
+ queryKey: key,
238
+ queryFn: () => {
239
+ return {
240
+ wow: true,
241
+ }
242
+ },
243
+ }))
244
+
245
+ expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
246
+ })
247
+
248
+ it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
249
+ const key = queryKey()
250
+ const { data } = injectQuery(() => ({
251
+ queryKey: key,
252
+ queryFn: () => {
253
+ return {
254
+ wow: true,
255
+ }
256
+ },
257
+ initialData: () => undefined as { wow: boolean } | undefined,
258
+ }))
259
+
260
+ expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
261
+ })
262
+
263
+ it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
264
+ const key = queryKey()
265
+ const query = injectQuery(() => ({
266
+ queryKey: key,
267
+ queryFn: () => {
268
+ return {
269
+ wow: true,
270
+ }
271
+ },
272
+ initialData: () => undefined as { wow: boolean } | undefined,
273
+ }))
274
+
275
+ if (query.isSuccess()) {
276
+ expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
277
+ }
278
+ })
279
+ })
280
+
281
+ describe('structuralSharing', () => {
282
+ it('should be able to use structuralSharing with unknown types', () => {
283
+ const key = queryKey()
284
+ // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
285
+ injectQuery(() => ({
286
+ queryKey: key,
287
+ queryFn: () => 5,
288
+ structuralSharing: (oldData, newData) => {
289
+ expectTypeOf(oldData).toBeUnknown()
290
+ expectTypeOf(newData).toBeUnknown()
291
+ return newData
292
+ },
293
+ }))
294
+ })
295
+ })
296
+ })
297
+
298
+ describe('Discriminated union return type', () => {
299
+ it('data should be possibly undefined by default', () => {
300
+ const key = queryKey()
301
+ const query = injectQuery(() => ({
302
+ queryKey: key,
303
+ queryFn: () => sleep(0).then(() => 'Some data'),
304
+ }))
305
+
306
+ expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
307
+ })
308
+
309
+ it('data should be defined when query is success', () => {
310
+ const key = queryKey()
311
+ const query = injectQuery(() => ({
312
+ queryKey: key,
313
+ queryFn: () => sleep(0).then(() => 'Some data'),
314
+ }))
315
+
316
+ if (query.isSuccess()) {
317
+ expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
318
+ }
319
+ })
320
+
321
+ it('error should be null when query is success', () => {
322
+ const key = queryKey()
323
+ const query = injectQuery(() => ({
324
+ queryKey: key,
325
+ queryFn: () => sleep(0).then(() => 'Some data'),
326
+ }))
327
+
328
+ if (query.isSuccess()) {
329
+ expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
330
+ }
331
+ })
332
+
333
+ it('data should be undefined when query is pending', () => {
334
+ const key = queryKey()
335
+ const query = injectQuery(() => ({
336
+ queryKey: key,
337
+ queryFn: () => sleep(0).then(() => 'Some data'),
338
+ }))
339
+
340
+ if (query.isPending()) {
341
+ expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
342
+ }
343
+ })
344
+
345
+ it('error should be defined when query is error', () => {
346
+ const key = queryKey()
347
+ const query = injectQuery(() => ({
348
+ queryKey: key,
349
+ queryFn: () => sleep(0).then(() => 'Some data'),
350
+ }))
351
+
352
+ if (query.isError()) {
353
+ expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
354
+ }
355
+ })
356
+ })
357
+ })