Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query-core): make MutateFunction optional undefinable-variables #8737

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions packages/query-core/src/__tests__/mutations.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { describe, expectTypeOf, it } from 'vitest'
import type { DefaultError, MutateFunction, MutateOptions } from 'src/types'

describe('MutateFunction', () => {
it('void variables', () => {
const mutate = {} as MutateFunction

expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
undefined | void
>()

expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
undefined | MutateOptions<unknown, DefaultError, void, unknown>
>()

mutate() // can be called with no arguments
mutate(undefined, {
onError: (e) => {
expectTypeOf(e).toEqualTypeOf<DefaultError>()
},
})
})

it('optional undefinable variables', () => {
const mutate = {} as MutateFunction<
unknown,
DefaultError,
number | undefined,
unknown
>

expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
number | undefined
>()

expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
| undefined
| MutateOptions<unknown, DefaultError, number | undefined, unknown>
>()

mutate() // can be called with no arguments
mutate(undefined, {
onError: (e) => {
expectTypeOf(e).toEqualTypeOf<DefaultError>()
},
})
})

it('required non-undefinable variables', () => {
const mutate = {} as MutateFunction<unknown, DefaultError, number, unknown>

expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<number>()

expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
undefined | MutateOptions<unknown, DefaultError, number, unknown>
>()

// @ts-expect-error --- required variables
mutate()
mutate(123, {
onError: (e) => {
expectTypeOf(e).toEqualTypeOf<DefaultError>()
},
})
})

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<MutateFunction>) => void

expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
undefined | void
>()

expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
undefined | MutateOptions<unknown, DefaultError, void, unknown>
>()

mutate() // can be called with no arguments
mutate(undefined, {
onError: (e) => {
expectTypeOf(e).toEqualTypeOf<DefaultError>()
},
})
})

it('optional undefinable variables', () => {
const mutate = {} as (
...options: Parameters<
MutateFunction<unknown, DefaultError, number | undefined, unknown>
>
) => void

expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
number | undefined
>()

expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
| undefined
| MutateOptions<unknown, DefaultError, number | undefined, unknown>
>()

mutate() // can be called with no arguments
mutate(undefined, {
onError: (e) => {
expectTypeOf(e).toEqualTypeOf<DefaultError>()
},
})
})

it('required non-undefinable variables', () => {
const mutate = {} as (
...options: Parameters<
MutateFunction<unknown, DefaultError, number, unknown>
>
) => void

expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<number>()

expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
undefined | MutateOptions<unknown, DefaultError, number, unknown>
>()

// @ts-expect-error --- required variables
mutate()
mutate(123, {
onError: (e) => {
expectTypeOf(e).toEqualTypeOf<DefaultError>()
},
})
})
})
})
18 changes: 16 additions & 2 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1161,14 +1161,28 @@ export interface MutateOptions<
) => void
}

export type MutateFunctionRest<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> = undefined extends TVariables
? [
variables?: TVariables,
options?: MutateOptions<TData, TError, TVariables, TContext>,
]
: [
variables: TVariables,
options?: MutateOptions<TData, TError, TVariables, TContext>,
]

export type MutateFunction<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> = (
variables: TVariables,
options?: MutateOptions<TData, TError, TVariables, TContext>,
...rest: MutateFunctionRest<TData, TError, TVariables, TContext>
) => Promise<TData>

export interface MutationObserverBaseResult<
Loading
Oops, something went wrong.