Skip to content

Commit 7c87c3d

Browse files
bviebahnBenedikt ViebahnTkDodoBenedikt Viebahn
authored
fix(types): pass TError, TQueryData and TQueryKey to PlaceholderDataFunction (#6349)
* fix: pass TError, TQueryData and TQueryKey to PlaceholderDataFunction * test: add types test for placeholderData function * style: format --------- Co-authored-by: Benedikt Viebahn <[email protected]> Co-authored-by: Dominik Dorfmeister <[email protected]> Co-authored-by: Benedikt Viebahn <[email protected]>
1 parent 8b7cc43 commit 7c87c3d

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, it } from 'vitest'
2+
import { QueryObserver } from '..'
3+
import { createQueryClient, doNotExecute } from './utils'
4+
import type { Equal, Expect } from './utils'
5+
6+
describe('placeholderData', () => {
7+
describe('placeholderData function', () => {
8+
it('previousQuery should have typed queryKey', () => {
9+
doNotExecute(() => {
10+
const queryKey = ['SomeQuery', 42, { foo: 'bar' }] as const
11+
type QueryKey = typeof queryKey
12+
13+
new QueryObserver(createQueryClient(), {
14+
queryKey,
15+
placeholderData: (_, previousQuery) => {
16+
const previousQueryKey = previousQuery?.queryKey
17+
18+
const result: Expect<
19+
Equal<typeof previousQueryKey, QueryKey | undefined>
20+
> = true
21+
return result
22+
},
23+
})
24+
})
25+
})
26+
27+
it('previousQuery should have typed error', () => {
28+
doNotExecute(() => {
29+
class CustomError extends Error {
30+
name = 'CustomError' as const
31+
}
32+
33+
new QueryObserver<boolean, CustomError>(createQueryClient(), {
34+
placeholderData: (_, previousQuery) => {
35+
const error = previousQuery?.state.error
36+
37+
const result: Expect<
38+
Equal<typeof error, CustomError | null | undefined>
39+
> = true
40+
return result
41+
},
42+
})
43+
})
44+
})
45+
46+
it('previousData should have the same type as query data', () => {
47+
doNotExecute(() => {
48+
const queryData = { foo: 'bar' } as const
49+
type QueryData = typeof queryData
50+
51+
new QueryObserver(createQueryClient(), {
52+
queryFn: () => queryData,
53+
select: (data) => data.foo,
54+
placeholderData: (previousData) => {
55+
const result: Expect<
56+
Equal<typeof previousData, QueryData | undefined>
57+
> = true
58+
return result ? previousData : undefined
59+
},
60+
})
61+
})
62+
})
63+
})
64+
})

packages/query-core/src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ export interface QueryObserverOptions<
325325
*/
326326
placeholderData?:
327327
| NonFunctionGuard<TQueryData>
328-
| PlaceholderDataFunction<NonFunctionGuard<TQueryData>>
328+
| PlaceholderDataFunction<
329+
NonFunctionGuard<TQueryData>,
330+
TError,
331+
NonFunctionGuard<TQueryData>,
332+
TQueryKey
333+
>
329334

330335
_optimisticResults?: 'optimistic' | 'isRestoring'
331336
}

0 commit comments

Comments
 (0)