Skip to content

Commit

Permalink
types: fix type regressions (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
promer94 committed May 14, 2022
1 parent 9a0328f commit 3cd796b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 27 deletions.
13 changes: 3 additions & 10 deletions _internal/types.ts
Expand Up @@ -174,7 +174,7 @@ export type Broadcaster<Data = any, Error = any> = (
populateCache?: boolean
) => Promise<Data>

export type State<Data, Error> = {
export type State<Data = any, Error = any> = {
data?: Data
error?: Error
isValidating?: boolean
Expand Down Expand Up @@ -266,18 +266,11 @@ export type RevalidateCallback = <K extends RevalidateEvent>(
) => RevalidateCallbackReturnType[K]

export interface Cache<Data = any> {
get(key: Key): CacheValue<Data> | undefined
set(key: Key, value: Data): void
get(key: Key): State<Data> | undefined
set(key: Key, value: State<Data>): void
delete(key: Key): void
}

export interface CacheValue<Data = any, Error = any> {
data?: Data
error?: Error
isValidating?: boolean
isLoading?: boolean
}

export interface StateDependencies {
data?: boolean
error?: boolean
Expand Down
4 changes: 2 additions & 2 deletions _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
Expand All @@ -24,7 +24,7 @@ export const hasRequestAnimationFrame = () =>
isWindowDefined && typeof window['requestAnimationFrame'] != STR_UNDEFINED

const EMPTY_CACHE = {}
export const createCacheHelper = <Data = any, T = CacheValue<Data, any>>(
export const createCacheHelper = <Data = any, T = State<Data, any>>(
cache: Cache,
key: Key
) => {
Expand Down
4 changes: 2 additions & 2 deletions _internal/utils/use-swr-config.ts
Expand Up @@ -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<string, CacheValue>
T extends Cache = Map<string, State>
>(): FullConfiguration<T> => {
return mergeObjects(defaultConfig, useContext(SWRConfigContext))
}
4 changes: 2 additions & 2 deletions core/index.ts
Expand Up @@ -18,11 +18,11 @@ export type {
SWRHook,
SWRResponse,
Cache,
CacheValue,
BareFetcher,
Fetcher,
MutatorCallback,
MutatorOptions,
Middleware,
Arguments
Arguments,
State
} from 'swr/_internal'
7 changes: 3 additions & 4 deletions core/use-swr.ts
Expand Up @@ -31,7 +31,6 @@ import {
SWRConfiguration,
SWRHook,
RevalidateEvent,
CacheValue,
StateDependencies
} from 'swr/_internal'

Expand Down Expand Up @@ -104,7 +103,7 @@ export const useSWRHandler = <Data = any, Error = any>(
? config.fallback[key]
: fallbackData

const selector = (snapshot: CacheValue<Data, any>) => {
const selector = (snapshot: State<Data, any>) => {
const shouldStartRequest = (() => {
if (!key) return false
if (!fetcher) return false
Expand All @@ -125,7 +124,7 @@ export const useSWRHandler = <Data = any, Error = any>(
return snapshot
}
const isEqual = useCallback(
(prev: CacheValue<Data, any>, current: CacheValue<Data, any>) => {
(prev: State<Data, any>, current: State<Data, any>) => {
let equal = true
for (const _ in stateDependencies) {
const t = _ as keyof StateDependencies
Expand All @@ -149,7 +148,7 @@ export const useSWRHandler = <Data = any, Error = any>(
const cached = useSyncExternalStoreWithSelector(
useCallback(
(callback: () => void) =>
subscribeCache(key, (current: CacheValue<Data, any>) => {
subscribeCache(key, (current: State<Data, any>) => {
stateRef.current = current
callback()
}),
Expand Down
4 changes: 2 additions & 2 deletions infinite/types.ts
Expand Up @@ -3,7 +3,7 @@ import {
SWRResponse,
Arguments,
BareFetcher,
CacheValue
State
} from 'swr/_internal'

type FetcherResponse<Data = unknown> = Data | Promise<Data>
Expand Down Expand Up @@ -119,7 +119,7 @@ export interface SWRInfiniteHook {
}

export interface SWRInfiniteCacheValue<Data = any, Error = any>
extends CacheValue<Data, Error> {
extends State<Data, Error> {
// 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]
Expand Down
8 changes: 3 additions & 5 deletions test/type/config.ts
@@ -1,14 +1,12 @@
import { useSWRConfig, Cache, CacheValue } from 'swr'
import { useSWRConfig, Cache, State } from 'swr'
import { expectType } from './utils'

interface CustomCache<Data = any> extends Cache<Data> {
reset(): void
}

export function useTestCache() {
expectType<Map<string, CacheValue>>(useSWRConfig().cache)
expectType<Map<string, CacheValue>>(
useSWRConfig<Map<string, CacheValue>>().cache
)
expectType<Map<string, State>>(useSWRConfig().cache)
expectType<Map<string, State>>(useSWRConfig<Map<string, State>>().cache)
expectType<CustomCache>(useSWRConfig<CustomCache>().cache)
}

0 comments on commit 3cd796b

Please sign in to comment.