Skip to content

Commit b327ad6

Browse files
committed
feat: force to set populateCache option with proper type when a value of different value is assigned to mutate
issue: vercel#2975
1 parent 69a47ab commit b327ad6

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

Diff for: src/_internal/types.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,24 @@ export interface ScopedMutator {
439439
* @typeParam Data - The type of the data related to the key
440440
* @typeParam MutationData - The type of the data returned by the mutator
441441
*/
442-
export type KeyedMutator<Data> = <MutationData = Data>(
443-
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
444-
opts?: boolean | MutatorOptions<Data, MutationData>
445-
) => Promise<Data | MutationData | undefined>
442+
export type KeyedMutator<Data> = {
443+
(
444+
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
445+
opts?: boolean | MutatorOptions<Data, Data>
446+
): Promise<Data | undefined>
447+
<MutationData = Data>(
448+
data:
449+
| MutationData
450+
| Promise<MutationData | undefined>
451+
| MutatorCallback<MutationData>,
452+
opts: Omit<MutatorOptions<Data, MutationData>, 'populateCache'> & {
453+
populateCache: (
454+
result: MutationData,
455+
currentData: Data | undefined
456+
) => Data
457+
}
458+
): Promise<Data | MutationData | undefined>
459+
}
446460

447461
export type SWRConfiguration<
448462
Data = any,

Diff for: src/infinite/index.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
import type {
2020
BareFetcher,
2121
SWRHook,
22-
MutatorCallback,
2322
Middleware,
2423
GlobalState
2524
} from '../_internal'
@@ -238,16 +237,9 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
238237
config
239238
)
240239

241-
const mutate = useCallback(
240+
const mutate = useCallback<SWRInfiniteKeyedMutator<Data[]>>(
242241
// eslint-disable-next-line func-names
243-
function <T = Data[]>(
244-
data?:
245-
| undefined
246-
| Data[]
247-
| Promise<Data[] | undefined>
248-
| MutatorCallback<Data[]>,
249-
opts?: undefined | boolean | SWRInfiniteMutatorOptions<Data[], T>
250-
) {
242+
function (data?: any, opts?: any) {
251243
// When passing as a boolean, it's explicitly used to disable/enable
252244
// revalidation.
253245
const options =

Diff for: src/infinite/types.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ interface SWRInfiniteRevalidateFn<Data = any> {
4747
(data: Data, key: Arguments): boolean
4848
}
4949

50-
export type SWRInfiniteKeyedMutator<Data> = <MutationData = Data>(
51-
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
52-
opts?: boolean | SWRInfiniteMutatorOptions<Data, MutationData>
53-
) => Promise<Data | MutationData | undefined>
50+
export type SWRInfiniteKeyedMutator<Data> = {
51+
(
52+
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
53+
opts?: boolean | SWRInfiniteMutatorOptions<Data, Data>
54+
): Promise<Data | undefined>
55+
<MutationData = Data>(
56+
data:
57+
| MutationData
58+
| Promise<MutationData | undefined>
59+
| MutatorCallback<MutationData>,
60+
opts: Omit<
61+
SWRInfiniteMutatorOptions<Data, MutationData>,
62+
'populateCache'
63+
> & {
64+
populateCache: (
65+
result: MutationData,
66+
currentData: Data | undefined
67+
) => Data
68+
}
69+
): Promise<Data | MutationData | undefined>
70+
}
5471

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

0 commit comments

Comments
 (0)