diff --git a/_internal/src/types.ts b/_internal/src/types.ts index e0215a7b5..13c0279cf 100644 --- a/_internal/src/types.ts +++ b/_internal/src/types.ts @@ -231,6 +231,10 @@ export interface SWRHook { key: SWRKey, fetcher: Fetcher | null ): SWRResponse + ( + key: SWRKey, + fetcher: Fetcher | null + ): SWRResponse < Data = any, Error = any, @@ -259,10 +263,6 @@ export interface SWRHook { config: SWROptions ): SWRResponse (key: Key): SWRResponse - ( - key: Key, - fetcher: BareFetcher | null - ): SWRResponse < Data = any, Error = any, @@ -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 - | Promise - | MutatorCallback, +export type KeyedMutator = ( + data?: Data | Promise | MutatorCallback, opts?: boolean | MutatorOptions -) => Promise +) => Promise export type SWRConfiguration< Data = any, diff --git a/core/src/use-swr.ts b/core/src/use-swr.ts index 8480ea805..2d40dc85a 100644 --- a/core/src/use-swr.ts +++ b/core/src/use-swr.ts @@ -5,7 +5,7 @@ import ReactExports, { useDebugValue, useMemo } from 'react' -import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js' +import { useSyncExternalStore } from 'use-sync-external-store/shim' import { defaultConfig, diff --git a/infinite/src/index.ts b/infinite/src/index.ts index 58089f31c..c2ae16cf0 100644 --- a/infinite/src/index.ts +++ b/infinite/src/index.ts @@ -33,7 +33,7 @@ import type { SWRInfiniteCacheValue, SWRInfiniteCompareFn } from './types' -import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js' +import { useSyncExternalStore } from 'use-sync-external-store/shim' import { getFirstPageKey } from './serialize' // const INFINITE_PREFIX = '$inf$' @@ -232,8 +232,12 @@ export const infinite = ((useSWRNext: SWRHook) => const mutate = useCallback( // eslint-disable-next-line func-names - function ( - data?: undefined | T | Promise | MutatorCallback, + function ( + data?: + | undefined + | Data[] + | Promise + | MutatorCallback, opts?: undefined | boolean | MutatorOptions ) { // When passing as a boolean, it's explicitly used to disable/enable @@ -257,8 +261,8 @@ export const infinite = ((useSWRNext: SWRHook) => } return arguments.length - ? swr.mutate(data, { ...options, revalidate: shouldRevalidate }) - : swr.mutate() + ? swr.mutate(data, { ...options, revalidate: shouldRevalidate }) + : swr.mutate() }, // swr.mutate is always the same reference // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/mutation/src/index.ts b/mutation/src/index.ts index 8386371d5..48cb1d0da 100644 --- a/mutation/src/index.ts +++ b/mutation/src/index.ts @@ -14,7 +14,10 @@ import type { SWRMutationConfiguration, SWRMutationResponse, SWRMutationHook, - MutationFetcher + MutationFetcher, + TriggerWithArgs, + TriggerWithoutArgs, + TriggerWithOptionsArgs } from './types' const mutation = (() => @@ -160,5 +163,8 @@ export { SWRMutationConfiguration, SWRMutationResponse, SWRMutationHook, - MutationFetcher + MutationFetcher, + TriggerWithArgs, + TriggerWithoutArgs, + TriggerWithOptionsArgs } diff --git a/mutation/src/types.ts b/mutation/src/types.ts index 086808a43..654ad4d4c 100644 --- a/mutation/src/types.ts +++ b/mutation/src/types.ts @@ -88,7 +88,7 @@ export interface TriggerWithArgs< ): Promise } -interface TriggerWithOptionsArgs< +export interface TriggerWithOptionsArgs< Data = any, Error = any, SWRMutationKey extends Key = Key, diff --git a/test/type/fetcher.ts b/test/type/fetcher.ts index 697d99daf..3a476aa04 100644 --- a/test/type/fetcher.ts +++ b/test/type/fetcher.ts @@ -6,6 +6,8 @@ import type { Equal } from '@type-challenges/utils' export function useDataErrorGeneric() { useSWR<{ id: number }>('/api/', () => ({ id: 123 })) useSWR('/api/', (key: string) => key) + const fetcher = ({ url }: { url: string }) => url + useSWR({ url: '/api' }, fetcher) useSWRInfinite( (index, previousPageData) => { expectType>(true) diff --git a/test/type/mutate.ts b/test/type/mutate.ts index 9b153c39c..020621cbf 100644 --- a/test/type/mutate.ts +++ b/test/type/mutate.ts @@ -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() { diff --git a/test/use-swr-local-mutation.test.tsx b/test/use-swr-local-mutation.test.tsx index 383d9f5ce..747404071 100644 --- a/test/use-swr-local-mutation.test.tsx +++ b/test/use-swr-local-mutation.test.tsx @@ -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(key, sendRequest('cherry'), { optimisticData: [...data, 'cherry (optimistic)'], populateCache: (result, currentData) => [ ...currentData,