diff --git a/src/_internal/types.ts b/src/_internal/types.ts index 7a85047a23..b65b1ab08e 100644 --- a/src/_internal/types.ts +++ b/src/_internal/types.ts @@ -439,13 +439,24 @@ export interface ScopedMutator { * @typeParam Data - The type of the data related to the key * @typeParam MutationData - The type of the data returned by the mutator */ -export type KeyedMutator = ( - data?: - | MutationData - | Promise - | MutatorCallback, - opts?: boolean | MutatorOptions -) => Promise +export type KeyedMutator = { + ( + data?: Data | Promise | MutatorCallback, + opts?: boolean | MutatorOptions + ): Promise + ( + data: + | MutationData + | Promise + | MutatorCallback, + opts: Omit, 'populateCache'> & { + populateCache: ( + result: MutationData, + currentData: Data | undefined + ) => Data + } + ): Promise +} export type SWRConfiguration< Data = any, diff --git a/src/infinite/index.ts b/src/infinite/index.ts index 6e51f85749..7c4204874c 100644 --- a/src/infinite/index.ts +++ b/src/infinite/index.ts @@ -19,7 +19,6 @@ import { import type { BareFetcher, SWRHook, - MutatorCallback, Middleware, GlobalState } from '../_internal' @@ -238,15 +237,9 @@ export const infinite = ((useSWRNext: SWRHook) => config ) - const mutate = useCallback( + const mutate = useCallback>( // eslint-disable-next-line func-names - function ( - data?: - | MutationData - | Promise - | MutatorCallback, - opts?: boolean | SWRInfiniteMutatorOptions - ) { + function (data?: any, opts?: any) { // When passing as a boolean, it's explicitly used to disable/enable // revalidation. const options = diff --git a/src/infinite/types.ts b/src/infinite/types.ts index 4237b6abae..2c5204d6c9 100644 --- a/src/infinite/types.ts +++ b/src/infinite/types.ts @@ -47,10 +47,27 @@ interface SWRInfiniteRevalidateFn { (data: Data, key: Arguments): boolean } -export type SWRInfiniteKeyedMutator = ( - data?: Data | Promise | MutatorCallback, - opts?: boolean | SWRInfiniteMutatorOptions -) => Promise +export type SWRInfiniteKeyedMutator = { + ( + data?: Data | Promise | MutatorCallback, + opts?: boolean | SWRInfiniteMutatorOptions + ): Promise + ( + data: + | MutationData + | Promise + | MutatorCallback, + opts: Omit< + SWRInfiniteMutatorOptions, + 'populateCache' + > & { + populateCache: ( + result: MutationData, + currentData: Data | undefined + ) => Data + } + ): Promise +} export interface SWRInfiniteMutatorOptions extends Omit, 'revalidate'> { diff --git a/test/type/mutate.ts b/test/type/mutate.ts index 08b6c18c69..94da25412c 100644 --- a/test/type/mutate.ts +++ b/test/type/mutate.ts @@ -62,11 +62,18 @@ export function useMutatorTypes() { mutate(async () => '1') mutate(async () => '1', { populateCache: false }) + mutate(async () => 1, { + populateCache: (result: number) => `${result}` + }) // @ts-expect-error mutate(async () => 1) // @ts-expect-error mutate(async () => 1, { populateCache: false }) + // @ts-expect-error + mutate(async () => 1, { + populateCache: (result: number) => result + }) } export function useConfigMutate() {