Skip to content

Commit e3b11c0

Browse files
committed
fix: narrow injectInfiniteQuery type with initial data
1 parent 94ee0eb commit e3b11c0

File tree

2 files changed

+116
-14
lines changed

2 files changed

+116
-14
lines changed

packages/angular-query-experimental/src/__tests__/inject-infinite-query.test-d.ts

Lines changed: 114 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,121 @@ describe('injectInfiniteQuery', () => {
2323
vi.useRealTimers()
2424
})
2525

26-
test('should narrow type after isSuccess', () => {
27-
const query = TestBed.runInInjectionContext(() => {
28-
return injectInfiniteQuery(() => ({
29-
queryKey: ['infiniteQuery'],
30-
queryFn: ({ pageParam }) =>
31-
sleep(0).then(() => 'data on page ' + pageParam),
32-
initialPageParam: 0,
33-
getNextPageParam: () => 12,
34-
}))
26+
describe('without initialData', () => {
27+
test('should narrow type after isSuccess', () => {
28+
const query = TestBed.runInInjectionContext(() => {
29+
return injectInfiniteQuery(() => ({
30+
queryKey: ['infiniteQuery'],
31+
queryFn: ({ pageParam }) =>
32+
sleep(0).then(() => 'data on page ' + pageParam),
33+
initialPageParam: 0,
34+
getNextPageParam: () => 12,
35+
}))
36+
})
37+
38+
if (query.isSuccess()) {
39+
const data = query.data()
40+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
41+
}
42+
})
43+
44+
test('should narrow type after isPending', () => {
45+
const query = TestBed.runInInjectionContext(() => {
46+
return injectInfiniteQuery(() => ({
47+
queryKey: ['infiniteQuery'],
48+
queryFn: ({ pageParam }) =>
49+
sleep(0).then(() => 'data on page ' + pageParam),
50+
initialPageParam: 0,
51+
getNextPageParam: () => 12,
52+
}))
53+
})
54+
55+
if (query.isPending()) {
56+
const data = query.data()
57+
expectTypeOf(data).toEqualTypeOf<undefined>()
58+
}
59+
})
60+
61+
test('should narrow type after isError', () => {
62+
const query = TestBed.runInInjectionContext(() => {
63+
return injectInfiniteQuery(() => ({
64+
queryKey: ['infiniteQuery'],
65+
queryFn: ({ pageParam }) =>
66+
sleep(0).then(() => 'data on page ' + pageParam),
67+
initialPageParam: 0,
68+
getNextPageParam: () => 12,
69+
}))
70+
})
71+
72+
if (query.isError()) {
73+
const error = query.error()
74+
expectTypeOf(error).toEqualTypeOf<Error>()
75+
}
76+
})
77+
})
78+
79+
describe('with initialData', () => {
80+
test('should narrow type after isSuccess', () => {
81+
const query = TestBed.runInInjectionContext(() => {
82+
return injectInfiniteQuery(() => ({
83+
queryKey: ['infiniteQuery'],
84+
queryFn: ({ pageParam }) =>
85+
sleep(0).then(() => 'data on page ' + pageParam),
86+
initialPageParam: 0,
87+
getNextPageParam: () => 12,
88+
initialData: {
89+
pageParams: [0, 1],
90+
pages: ['page 0 data', 'page 1 data'],
91+
},
92+
}))
93+
})
94+
95+
if (query.isSuccess()) {
96+
const data = query.data()
97+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
98+
}
99+
})
100+
101+
test('should narrow type after isPending', () => {
102+
const query = TestBed.runInInjectionContext(() => {
103+
return injectInfiniteQuery(() => ({
104+
queryKey: ['infiniteQuery'],
105+
queryFn: ({ pageParam }) =>
106+
sleep(0).then(() => 'data on page ' + pageParam),
107+
initialPageParam: 0,
108+
getNextPageParam: () => 12,
109+
initialData: {
110+
pageParams: [0, 1],
111+
pages: ['page 0 data', 'page 1 data'],
112+
},
113+
}))
114+
})
115+
116+
if (query.isPending()) {
117+
const data = query.data()
118+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
119+
}
35120
})
36121

37-
if (query.isSuccess()) {
38-
const data = query.data()
39-
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
40-
}
122+
test('should narrow type after isError', () => {
123+
const query = TestBed.runInInjectionContext(() => {
124+
return injectInfiniteQuery(() => ({
125+
queryKey: ['infiniteQuery'],
126+
queryFn: ({ pageParam }) =>
127+
sleep(0).then(() => 'data on page ' + pageParam),
128+
initialPageParam: 0,
129+
getNextPageParam: () => 12,
130+
initialData: {
131+
pageParams: [0, 1],
132+
pages: ['page 0 data', 'page 1 data'],
133+
},
134+
}))
135+
})
136+
137+
if (query.isError()) {
138+
const error = query.error()
139+
expectTypeOf(error).toEqualTypeOf<Error>()
140+
}
141+
})
41142
})
42143
})

packages/angular-query-experimental/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ export type DefinedCreateInfiniteQueryResult<
157157
TData,
158158
TError
159159
>,
160-
> = MapToSignals<TDefinedInfiniteQueryObserver>
160+
> =BaseQueryNarrowing<TData, TError> &
161+
MapToSignals<TDefinedInfiniteQueryObserver>
161162

162163
export interface CreateMutationOptions<
163164
TData = unknown,

0 commit comments

Comments
 (0)