Skip to content

Commit

Permalink
fix: keyedmutator type
Browse files Browse the repository at this point in the history
  • Loading branch information
promer94 committed Sep 10, 2023
1 parent 1de4d2e commit 67dab71
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 2 additions & 5 deletions _internal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,9 @@ export interface ScopedMutator {
* @typeParam MutationData - The type of the data returned by the mutator
*/
export type KeyedMutator<Data> = <MutationData = Data>(
data?:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data, MutationData>
) => Promise<MutationData | undefined>
) => Promise<Data | MutationData | undefined>

export type SWRConfiguration<
Data = any,
Expand Down
12 changes: 8 additions & 4 deletions infinite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,12 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>

const mutate = useCallback(
// eslint-disable-next-line func-names
function <T>(
data?: undefined | T | Promise<T | undefined> | MutatorCallback<T>,
function <T = Data[]>(
data?:
| undefined
| Data[]
| Promise<Data[] | undefined>
| MutatorCallback<Data[]>,
opts?: undefined | boolean | MutatorOptions<Data[], T>
) {
// When passing as a boolean, it's explicitly used to disable/enable
Expand All @@ -257,8 +261,8 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
}

return arguments.length
? swr.mutate<T>(data, { ...options, revalidate: shouldRevalidate })
: swr.mutate<T>()
? swr.mutate(data, { ...options, revalidate: shouldRevalidate })
: swr.mutate()
},
// swr.mutate is always the same reference
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
4 changes: 3 additions & 1 deletion test/type/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ export function useMutatorTypes() {

mutate(async () => '1')

// @ts-expect-error
mutate(async () => 1)

mutate(async () => 1, { populateCache: false })
// FIXME: this should work.
// mutate(async () => 1, { populateCache: false })
}

export function useConfigMutate() {
Expand Down

0 comments on commit 67dab71

Please sign in to comment.