Skip to content

Commit 6384368

Browse files
test(vue-query/mutationOptions): add type tests for getter overloads (#10466)
* test(vue-query/mutationOptions): add type tests for getter overloads * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 24de260 commit 6384368

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

packages/vue-query/src/__tests__/mutationOptions.test-d.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,56 @@ describe('mutationOptions', () => {
243243
const resolved = options()
244244
expectTypeOf(resolved.mutationFn).not.toBeUndefined()
245245
})
246+
247+
it('should error if mutationFn return type mismatches TData (getter)', () => {
248+
assertType(
249+
mutationOptions<number>(() => ({
250+
// @ts-expect-error this is a good error, because return type is string, not number
251+
mutationFn: async () => Promise.resolve('wrong return'),
252+
})),
253+
)
254+
})
255+
256+
it('should infer all types when not explicitly provided (getter)', () => {
257+
expectTypeOf(
258+
mutationOptions(() => ({
259+
mutationFn: (id: string) => Promise.resolve(id.length),
260+
mutationKey: ['key'],
261+
onSuccess: (data) => {
262+
expectTypeOf(data).toEqualTypeOf<number>()
263+
},
264+
})),
265+
).toEqualTypeOf<
266+
() => WithRequired<
267+
MutationOptions<number, DefaultError, string, unknown>,
268+
'mutationKey'
269+
>
270+
>()
271+
expectTypeOf(
272+
mutationOptions(() => ({
273+
mutationFn: (id: string) => Promise.resolve(id.length),
274+
onSuccess: (data) => {
275+
expectTypeOf(data).toEqualTypeOf<number>()
276+
},
277+
})),
278+
).toEqualTypeOf<
279+
() => Omit<
280+
MutationOptions<number, DefaultError, string, unknown>,
281+
'mutationKey'
282+
>
283+
>()
284+
})
285+
286+
it('should work when used with useMutation (getter)', () => {
287+
const mutation = useMutation(
288+
mutationOptions(() => ({
289+
mutationKey: ['key'],
290+
mutationFn: () => Promise.resolve('data'),
291+
onSuccess: (data) => {
292+
expectTypeOf(data).toEqualTypeOf<string>()
293+
},
294+
})),
295+
)
296+
expectTypeOf(mutation.data.value).toEqualTypeOf<string | undefined>()
297+
})
246298
})

0 commit comments

Comments
 (0)