Skip to content

Commit

Permalink
fix: default to fetch type in keyed mutator (#2753)
Browse files Browse the repository at this point in the history
* fix: default to data type in keyed mutator

* fix: keyedmutator type

* test: should use global mutate here

---------

Co-authored-by: Yixuan Xu <[email protected]>
  • Loading branch information
linkvt and promer94 committed Sep 10, 2023
1 parent a2bb5ae commit efe07d7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
9 changes: 3 additions & 6 deletions _internal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,10 @@ 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>(
data?:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
export type KeyedMutator<Data> = <MutationData = Data>(
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
5 changes: 3 additions & 2 deletions test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1580,10 +1580,11 @@ describe('useSWR - local mutation', () => {
}

function Page() {
const { data, mutate } = useSWR(key, () => serverData)
const { mutate } = useSWRConfig()
const { data } = useSWR(key, () => serverData)

appendData = () => {
return mutate(sendRequest('cherry'), {
return mutate<string[], string>(key, sendRequest('cherry'), {
optimisticData: [...data, 'cherry (optimistic)'],
populateCache: (result, currentData) => [
...currentData,
Expand Down

0 comments on commit efe07d7

Please sign in to comment.