diff --git a/packages/query-core/src/__tests__/mutation.test-d.tsx b/packages/query-core/src/__tests__/mutation.test-d.tsx index 49397c39629..55ed9cea967 100644 --- a/packages/query-core/src/__tests__/mutation.test-d.tsx +++ b/packages/query-core/src/__tests__/mutation.test-d.tsx @@ -1,6 +1,7 @@ import { describe, expectTypeOf, it } from 'vitest' import { QueryClient } from '../queryClient' import { MutationObserver } from '../mutationObserver' +import type { DefaultError, MutateFunction, MutateOptions } from '../types' describe('mutation', () => { describe('onMutate', () => { @@ -93,4 +94,241 @@ describe('mutation', () => { }) }) }) + + describe('MutateFunction', () => { + it('void variables', () => { + const mutate = {} as MutateFunction + + expectTypeOf[0]>().toEqualTypeOf< + undefined | void + >() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + mutate(undefined, { + onError: (e) => { + expectTypeOf(e).toEqualTypeOf() + }, + }) + }) + + it('optional undefinable variables', () => { + const mutate = {} as MutateFunction< + unknown, + DefaultError, + number | undefined, + unknown + > + + expectTypeOf[0]>().toEqualTypeOf< + number | undefined + >() + + expectTypeOf[1]>().toEqualTypeOf< + | undefined + | MutateOptions + >() + + mutate() // can be called with no arguments + mutate(undefined, { + onError: (e) => { + expectTypeOf(e).toEqualTypeOf() + }, + }) + }) + + it('unknown variables', () => { + const mutate = {} as MutateFunction< + unknown, + DefaultError, + unknown, + unknown + > + + expectTypeOf[0]>().toEqualTypeOf() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + }) + + it('any variables', () => { + const mutate = {} as MutateFunction + + expectTypeOf[0]>().toEqualTypeOf() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + }) + + it('void union variables', () => { + const mutate = {} as MutateFunction< + unknown, + DefaultError, + void | string, + unknown + > + + expectTypeOf[0]>().toEqualTypeOf< + void | string | undefined + >() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + }) + + it('required non-undefinable variables', () => { + const mutate = {} as MutateFunction< + unknown, + DefaultError, + number, + unknown + > + + expectTypeOf[0]>().toEqualTypeOf() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + // @ts-expect-error --- required variables + mutate() + mutate(123, { + onError: (e) => { + expectTypeOf(e).toEqualTypeOf() + }, + }) + }) + + describe('compatible with spread arguments pattern', () => { + // this is common pattern used internal so we need make sure it still works + + it('void variables', () => { + const mutate = {} as (...options: Parameters) => void + + expectTypeOf[0]>().toEqualTypeOf< + undefined | void + >() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + mutate(undefined, { + onError: (e) => { + expectTypeOf(e).toEqualTypeOf() + }, + }) + }) + + it('optional undefinable variables', () => { + const mutate = {} as ( + ...options: Parameters< + MutateFunction + > + ) => void + + expectTypeOf[0]>().toEqualTypeOf< + number | undefined + >() + + expectTypeOf[1]>().toEqualTypeOf< + | undefined + | MutateOptions + >() + + mutate() // can be called with no arguments + mutate(undefined, { + onError: (e) => { + expectTypeOf(e).toEqualTypeOf() + }, + }) + }) + + it('unknown variables', () => { + const mutate = {} as ( + ...options: Parameters< + MutateFunction + > + ) => void + + expectTypeOf[0]>().toEqualTypeOf() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + }) + + it('any variables', () => { + const mutate = {} as ( + ...options: Parameters< + MutateFunction + > + ) => void + + expectTypeOf[0]>().toEqualTypeOf() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + mutate() // can be called with no arguments + }) + + it('void union variables', () => { + const mutate = {} as ( + ...options: Parameters< + MutateFunction + > + ) => void + + expectTypeOf[0]>().toEqualTypeOf< + void | string | undefined + >() + + expectTypeOf[1]>().toEqualTypeOf< + | undefined + | MutateOptions + >() + + mutate() // can be called with no arguments + }) + + it('required non-undefinable variables', () => { + const mutate = {} as ( + ...options: Parameters< + MutateFunction + > + ) => void + + expectTypeOf[0]>().toEqualTypeOf() + + expectTypeOf[1]>().toEqualTypeOf< + undefined | MutateOptions + >() + + // @ts-expect-error --- required variables + mutate() + mutate(123, { + onError: (e) => { + expectTypeOf(e).toEqualTypeOf() + }, + }) + }) + }) + }) }) diff --git a/packages/query-core/src/__tests__/mutations.test.tsx b/packages/query-core/src/__tests__/mutation.test.tsx similarity index 99% rename from packages/query-core/src/__tests__/mutations.test.tsx rename to packages/query-core/src/__tests__/mutation.test.tsx index 8ad7f644779..6fdd1d5ebd0 100644 --- a/packages/query-core/src/__tests__/mutations.test.tsx +++ b/packages/query-core/src/__tests__/mutation.test.tsx @@ -5,7 +5,7 @@ import { MutationObserver } from '../mutationObserver' import { executeMutation } from './utils' import type { MutationState } from '../mutation' -describe('mutations', () => { +describe('mutation', () => { let queryClient: QueryClient beforeEach(() => { diff --git a/packages/query-core/src/__tests__/mutations.test-d.tsx b/packages/query-core/src/__tests__/mutations.test-d.tsx deleted file mode 100644 index 53465d601cf..00000000000 --- a/packages/query-core/src/__tests__/mutations.test-d.tsx +++ /dev/null @@ -1,229 +0,0 @@ -import { describe, expectTypeOf, it } from 'vitest' - -import type { DefaultError, MutateFunction, MutateOptions } from '../types' - -describe('MutateFunction', () => { - it('void variables', () => { - const mutate = {} as MutateFunction - - expectTypeOf[0]>().toEqualTypeOf< - undefined | void - >() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - mutate(undefined, { - onError: (e) => { - expectTypeOf(e).toEqualTypeOf() - }, - }) - }) - - it('optional undefinable variables', () => { - const mutate = {} as MutateFunction< - unknown, - DefaultError, - number | undefined, - unknown - > - - expectTypeOf[0]>().toEqualTypeOf< - number | undefined - >() - - expectTypeOf[1]>().toEqualTypeOf< - | undefined - | MutateOptions - >() - - mutate() // can be called with no arguments - mutate(undefined, { - onError: (e) => { - expectTypeOf(e).toEqualTypeOf() - }, - }) - }) - - it('unknown variables', () => { - const mutate = {} as MutateFunction - - expectTypeOf[0]>().toEqualTypeOf() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - }) - - it('any variables', () => { - const mutate = {} as MutateFunction - - expectTypeOf[0]>().toEqualTypeOf() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - }) - - it('void union variables', () => { - const mutate = {} as MutateFunction< - unknown, - DefaultError, - void | string, - unknown - > - - expectTypeOf[0]>().toEqualTypeOf< - void | string | undefined - >() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - }) - - it('required non-undefinable variables', () => { - const mutate = {} as MutateFunction - - expectTypeOf[0]>().toEqualTypeOf() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - // @ts-expect-error --- required variables - mutate() - mutate(123, { - onError: (e) => { - expectTypeOf(e).toEqualTypeOf() - }, - }) - }) - - describe('compatible with spread arguments pattern', () => { - // this is common pattern used internal so we need make sure it still works - - it('void variables', () => { - const mutate = {} as (...options: Parameters) => void - - expectTypeOf[0]>().toEqualTypeOf< - undefined | void - >() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - mutate(undefined, { - onError: (e) => { - expectTypeOf(e).toEqualTypeOf() - }, - }) - }) - - it('optional undefinable variables', () => { - const mutate = {} as ( - ...options: Parameters< - MutateFunction - > - ) => void - - expectTypeOf[0]>().toEqualTypeOf< - number | undefined - >() - - expectTypeOf[1]>().toEqualTypeOf< - | undefined - | MutateOptions - >() - - mutate() // can be called with no arguments - mutate(undefined, { - onError: (e) => { - expectTypeOf(e).toEqualTypeOf() - }, - }) - }) - - it('unknown variables', () => { - const mutate = {} as ( - ...options: Parameters< - MutateFunction - > - ) => void - - expectTypeOf[0]>().toEqualTypeOf() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - }) - - it('any variables', () => { - const mutate = {} as ( - ...options: Parameters< - MutateFunction - > - ) => void - - expectTypeOf[0]>().toEqualTypeOf() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - }) - - it('void union variables', () => { - const mutate = {} as ( - ...options: Parameters< - MutateFunction - > - ) => void - - expectTypeOf[0]>().toEqualTypeOf< - void | string | undefined - >() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - mutate() // can be called with no arguments - }) - - it('required non-undefinable variables', () => { - const mutate = {} as ( - ...options: Parameters< - MutateFunction - > - ) => void - - expectTypeOf[0]>().toEqualTypeOf() - - expectTypeOf[1]>().toEqualTypeOf< - undefined | MutateOptions - >() - - // @ts-expect-error --- required variables - mutate() - mutate(123, { - onError: (e) => { - expectTypeOf(e).toEqualTypeOf() - }, - }) - }) - }) -})