diff --git a/packages/vue-query/src/__tests__/mutationOptions.test-d.ts b/packages/vue-query/src/__tests__/mutationOptions.test-d.ts index 9f0d1fe4a7..14d19a9f43 100644 --- a/packages/vue-query/src/__tests__/mutationOptions.test-d.ts +++ b/packages/vue-query/src/__tests__/mutationOptions.test-d.ts @@ -243,4 +243,56 @@ describe('mutationOptions', () => { const resolved = options() expectTypeOf(resolved.mutationFn).not.toBeUndefined() }) + + it('should error if mutationFn return type mismatches TData (getter)', () => { + assertType( + mutationOptions(() => ({ + // @ts-expect-error this is a good error, because return type is string, not number + mutationFn: async () => Promise.resolve('wrong return'), + })), + ) + }) + + it('should infer all types when not explicitly provided (getter)', () => { + expectTypeOf( + mutationOptions(() => ({ + mutationFn: (id: string) => Promise.resolve(id.length), + mutationKey: ['key'], + onSuccess: (data) => { + expectTypeOf(data).toEqualTypeOf() + }, + })), + ).toEqualTypeOf< + () => WithRequired< + MutationOptions, + 'mutationKey' + > + >() + expectTypeOf( + mutationOptions(() => ({ + mutationFn: (id: string) => Promise.resolve(id.length), + onSuccess: (data) => { + expectTypeOf(data).toEqualTypeOf() + }, + })), + ).toEqualTypeOf< + () => Omit< + MutationOptions, + 'mutationKey' + > + >() + }) + + it('should work when used with useMutation (getter)', () => { + const mutation = useMutation( + mutationOptions(() => ({ + mutationKey: ['key'], + mutationFn: () => Promise.resolve('data'), + onSuccess: (data) => { + expectTypeOf(data).toEqualTypeOf() + }, + })), + ) + expectTypeOf(mutation.data.value).toEqualTypeOf() + }) })