From 700a3c8d20aff0ecf964d2e5d8e17015b8c285cc Mon Sep 17 00:00:00 2001 From: Yixuan Xu Date: Sat, 14 May 2022 05:15:40 +0800 Subject: [PATCH] types: fix type regressions --- _internal/types.ts | 13 +++---------- _internal/utils/helper.ts | 4 ++-- _internal/utils/use-swr-config.ts | 4 ++-- core/index.ts | 4 ++-- core/use-swr.ts | 7 +++---- infinite/types.ts | 4 ++-- test/type/config.ts | 8 +++----- 7 files changed, 17 insertions(+), 27 deletions(-) diff --git a/_internal/types.ts b/_internal/types.ts index ec0c20817..d755d66df 100644 --- a/_internal/types.ts +++ b/_internal/types.ts @@ -174,7 +174,7 @@ export type Broadcaster = ( populateCache?: boolean ) => Promise -export type State = { +export type State = { data?: Data error?: Error isValidating?: boolean @@ -266,18 +266,11 @@ export type RevalidateCallback = ( ) => RevalidateCallbackReturnType[K] export interface Cache { - get(key: Key): CacheValue | undefined - set(key: Key, value: Data): void + get(key: Key): State | undefined + set(key: Key, value: State): void delete(key: Key): void } -export interface CacheValue { - data?: Data - error?: Error - isValidating?: boolean - isLoading?: boolean -} - export interface StateDependencies { data?: boolean error?: boolean diff --git a/_internal/utils/helper.ts b/_internal/utils/helper.ts index 6ff4f9114..06d9db5a8 100644 --- a/_internal/utils/helper.ts +++ b/_internal/utils/helper.ts @@ -1,5 +1,5 @@ import { SWRGlobalState } from './global-state' -import { Key, Cache, CacheValue, GlobalState } from '../types' +import { Key, Cache, State, GlobalState } from '../types' export const noop = () => {} // Using noop() as the undefined value as undefined can possibly be replaced @@ -24,7 +24,7 @@ export const hasRequestAnimationFrame = () => isWindowDefined && typeof window['requestAnimationFrame'] != STR_UNDEFINED const EMPTY_CACHE = {} -export const createCacheHelper = >( +export const createCacheHelper = >( cache: Cache, key: Key ) => { diff --git a/_internal/utils/use-swr-config.ts b/_internal/utils/use-swr-config.ts index 89197197a..8181ff407 100644 --- a/_internal/utils/use-swr-config.ts +++ b/_internal/utils/use-swr-config.ts @@ -2,10 +2,10 @@ import { useContext } from 'react' import { defaultConfig } from './config' import { SWRConfigContext } from './config-context' import { mergeObjects } from './helper' -import { FullConfiguration, Cache, CacheValue } from '../types' +import { FullConfiguration, Cache, State } from '../types' export const useSWRConfig = < - T extends Cache = Map + T extends Cache = Map >(): FullConfiguration => { return mergeObjects(defaultConfig, useContext(SWRConfigContext)) } diff --git a/core/index.ts b/core/index.ts index 729252f1a..893a0d033 100644 --- a/core/index.ts +++ b/core/index.ts @@ -18,11 +18,11 @@ export type { SWRHook, SWRResponse, Cache, - CacheValue, BareFetcher, Fetcher, MutatorCallback, MutatorOptions, Middleware, - Arguments + Arguments, + State } from 'swr/_internal' diff --git a/core/use-swr.ts b/core/use-swr.ts index 48207c042..e613d6b97 100644 --- a/core/use-swr.ts +++ b/core/use-swr.ts @@ -31,7 +31,6 @@ import { SWRConfiguration, SWRHook, RevalidateEvent, - CacheValue, StateDependencies } from 'swr/_internal' @@ -104,7 +103,7 @@ export const useSWRHandler = ( ? config.fallback[key] : fallbackData - const selector = (snapshot: CacheValue) => { + const selector = (snapshot: State) => { const shouldStartRequest = (() => { if (!key) return false if (!fetcher) return false @@ -125,7 +124,7 @@ export const useSWRHandler = ( return snapshot } const isEqual = useCallback( - (prev: CacheValue, current: CacheValue) => { + (prev: State, current: State) => { let equal = true for (const _ in stateDependencies) { const t = _ as keyof StateDependencies @@ -149,7 +148,7 @@ export const useSWRHandler = ( const cached = useSyncExternalStoreWithSelector( useCallback( (callback: () => void) => - subscribeCache(key, (current: CacheValue) => { + subscribeCache(key, (current: State) => { stateRef.current = current callback() }), diff --git a/infinite/types.ts b/infinite/types.ts index ae99d6b41..70d3fbb8a 100644 --- a/infinite/types.ts +++ b/infinite/types.ts @@ -3,7 +3,7 @@ import { SWRResponse, Arguments, BareFetcher, - CacheValue + State } from 'swr/_internal' type FetcherResponse = Data | Promise @@ -119,7 +119,7 @@ export interface SWRInfiniteHook { } export interface SWRInfiniteCacheValue - extends CacheValue { + extends State { // We use cache to pass extra info (context) to fetcher so it can be globally // shared. The key of the context data is based on the first page key. $ctx?: [boolean] | [boolean, Data[] | undefined] diff --git a/test/type/config.ts b/test/type/config.ts index 1a8c99201..4685d4c69 100644 --- a/test/type/config.ts +++ b/test/type/config.ts @@ -1,4 +1,4 @@ -import { useSWRConfig, Cache, CacheValue } from 'swr' +import { useSWRConfig, Cache, State } from 'swr' import { expectType } from './utils' interface CustomCache extends Cache { @@ -6,9 +6,7 @@ interface CustomCache extends Cache { } export function useTestCache() { - expectType>(useSWRConfig().cache) - expectType>( - useSWRConfig>().cache - ) + expectType>(useSWRConfig().cache) + expectType>(useSWRConfig>().cache) expectType(useSWRConfig().cache) }