Skip to content

Commit

Permalink
feat: force to set populateCache option with proper type when a val…
Browse files Browse the repository at this point in the history
…ue of different value is assigned to mutate

issue: vercel#2975
  • Loading branch information
Key5n committed Jun 17, 2024
1 parent 69a47ab commit b327ad6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
22 changes: 18 additions & 4 deletions src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +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 = Data>(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data, MutationData>
) => Promise<Data | MutationData | undefined>
export type KeyedMutator<Data> = {
(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data, Data>
): Promise<Data | undefined>
<MutationData = Data>(
data:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
opts: Omit<MutatorOptions<Data, MutationData>, 'populateCache'> & {
populateCache: (
result: MutationData,
currentData: Data | undefined
) => Data
}
): Promise<Data | MutationData | undefined>
}

export type SWRConfiguration<
Data = any,
Expand Down
12 changes: 2 additions & 10 deletions src/infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import type {
BareFetcher,
SWRHook,
MutatorCallback,
Middleware,
GlobalState
} from '../_internal'
Expand Down Expand Up @@ -238,16 +237,9 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
config
)

const mutate = useCallback(
const mutate = useCallback<SWRInfiniteKeyedMutator<Data[]>>(
// eslint-disable-next-line func-names
function <T = Data[]>(
data?:
| undefined
| Data[]
| Promise<Data[] | undefined>
| MutatorCallback<Data[]>,
opts?: undefined | boolean | SWRInfiniteMutatorOptions<Data[], T>
) {
function (data?: any, opts?: any) {
// When passing as a boolean, it's explicitly used to disable/enable
// revalidation.
const options =
Expand Down
25 changes: 21 additions & 4 deletions src/infinite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,27 @@ interface SWRInfiniteRevalidateFn<Data = any> {
(data: Data, key: Arguments): boolean
}

export type SWRInfiniteKeyedMutator<Data> = <MutationData = Data>(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | SWRInfiniteMutatorOptions<Data, MutationData>
) => Promise<Data | MutationData | undefined>
export type SWRInfiniteKeyedMutator<Data> = {
(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | SWRInfiniteMutatorOptions<Data, Data>
): Promise<Data | undefined>
<MutationData = Data>(
data:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
opts: Omit<
SWRInfiniteMutatorOptions<Data, MutationData>,
'populateCache'
> & {
populateCache: (
result: MutationData,
currentData: Data | undefined
) => Data
}
): Promise<Data | MutationData | undefined>
}

export interface SWRInfiniteMutatorOptions<Data = any, MutationData = Data>
extends Omit<MutatorOptions<Data, MutationData>, 'revalidate'> {
Expand Down

0 comments on commit b327ad6

Please sign in to comment.