From d2a4282af1300b7043a5dce4d9e8265a1a86b57d Mon Sep 17 00:00:00 2001 From: Key5n Date: Mon, 8 Apr 2024 22:23:38 +0900 Subject: [PATCH 1/5] fix: fix the type of the argument of `KeyedMutator` for `populateCache` --- src/_internal/types.ts | 7 +++++-- src/infinite/index.ts | 11 +++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/_internal/types.ts b/src/_internal/types.ts index 106e26333..ddc97b599 100644 --- a/src/_internal/types.ts +++ b/src/_internal/types.ts @@ -395,9 +395,12 @@ export interface ScopedMutator { * @typeParam MutationData - The type of the data returned by the mutator */ export type KeyedMutator = ( - data?: Data | Promise | MutatorCallback, + data?: + | MutationData + | Promise + | MutatorCallback, opts?: boolean | MutatorOptions -) => Promise +) => Promise export type SWRConfiguration< Data = any, diff --git a/src/infinite/index.ts b/src/infinite/index.ts index 99e4d2b40..e7c135439 100644 --- a/src/infinite/index.ts +++ b/src/infinite/index.ts @@ -239,13 +239,12 @@ export const infinite = ((useSWRNext: SWRHook) => const mutate = useCallback( // eslint-disable-next-line func-names - function ( + function ( data?: - | undefined - | Data[] - | Promise - | MutatorCallback, - opts?: undefined | boolean | SWRInfiniteMutatorOptions + | MutationData + | Promise + | MutatorCallback, + opts?: boolean | SWRInfiniteMutatorOptions ) { // When passing as a boolean, it's explicitly used to disable/enable // revalidation. From cc1ac3e0af84b8d6a0eb8648616284c2427beeaa Mon Sep 17 00:00:00 2001 From: Key5n Date: Mon, 8 Apr 2024 22:26:59 +0900 Subject: [PATCH 2/5] refactor: change generics parameter name for consistency --- src/_internal/types.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_internal/types.ts b/src/_internal/types.ts index ddc97b599..f1c15f0b8 100644 --- a/src/_internal/types.ts +++ b/src/_internal/types.ts @@ -383,11 +383,11 @@ 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 */ - ( + ( key: Arguments, - data?: T | Promise | MutatorCallback, - opts?: boolean | MutatorOptions - ): Promise + data?: MutationData | Promise | MutatorCallback, + opts?: boolean | MutatorOptions + ): Promise } /** From 89611e3a8d302383277f3109c2a75055dfea5114 Mon Sep 17 00:00:00 2001 From: Key5n Date: Fri, 14 Jun 2024 20:18:41 +0900 Subject: [PATCH 3/5] Revert "fix: fix the type of the argument of `KeyedMutator` for `populateCache`" This reverts commit d2a4282af1300b7043a5dce4d9e8265a1a86b57d. --- src/_internal/types.ts | 7 ++----- src/infinite/index.ts | 11 ++++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/_internal/types.ts b/src/_internal/types.ts index 7a85047a2..f3cdba5ed 100644 --- a/src/_internal/types.ts +++ b/src/_internal/types.ts @@ -440,12 +440,9 @@ export interface ScopedMutator { * @typeParam MutationData - The type of the data returned by the mutator */ export type KeyedMutator = ( - data?: - | MutationData - | Promise - | MutatorCallback, + data?: Data | Promise | MutatorCallback, opts?: boolean | MutatorOptions -) => Promise +) => Promise export type SWRConfiguration< Data = any, diff --git a/src/infinite/index.ts b/src/infinite/index.ts index 6e51f8574..f410b5bef 100644 --- a/src/infinite/index.ts +++ b/src/infinite/index.ts @@ -240,12 +240,13 @@ export const infinite = ((useSWRNext: SWRHook) => const mutate = useCallback( // eslint-disable-next-line func-names - function ( + function ( data?: - | MutationData - | Promise - | MutatorCallback, - opts?: boolean | SWRInfiniteMutatorOptions + | undefined + | Data[] + | Promise + | MutatorCallback, + opts?: undefined | boolean | SWRInfiniteMutatorOptions ) { // When passing as a boolean, it's explicitly used to disable/enable // revalidation. From 8ea098d357ba04da66fc4656641fb967efea637a Mon Sep 17 00:00:00 2001 From: Key5n Date: Mon, 17 Jun 2024 23:58:12 +0900 Subject: [PATCH 4/5] feat: force to set `populateCache` option with proper type when a value of different value is assigned to mutate issue: https://github.com/vercel/swr/issues/2975 --- src/_internal/types.ts | 22 ++++++++++++++++++---- src/infinite/index.ts | 12 ++---------- src/infinite/types.ts | 25 +++++++++++++++++++++---- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/_internal/types.ts b/src/_internal/types.ts index f3cdba5ed..b65b1ab08 100644 --- a/src/_internal/types.ts +++ b/src/_internal/types.ts @@ -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?: Data | 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 f410b5bef..7c4204874 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,16 +237,9 @@ export const infinite = ((useSWRNext: SWRHook) => config ) - const mutate = useCallback( + const mutate = useCallback>( // eslint-disable-next-line func-names - function ( - data?: - | undefined - | Data[] - | Promise - | MutatorCallback, - opts?: undefined | 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 4237b6aba..2c5204d6c 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'> { From 52cf67dfb5562435aaa9c05ff5dfc89e7c22ec40 Mon Sep 17 00:00:00 2001 From: Key5n Date: Tue, 18 Jun 2024 00:05:14 +0900 Subject: [PATCH 5/5] test: test for mutate and populateCache when a value of different type is assgined --- test/type/mutate.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/type/mutate.ts b/test/type/mutate.ts index 08b6c18c6..94da25412 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() {