New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: infer provider type as cache type #1506
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit bdddf3a:
|
get(key: Key): Data | null | undefined | ||
set(key: Key, value: Data): void | ||
delete(key: Key): void | ||
} | ||
|
||
export type DefaultProvider = Map<string | null, any> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why string | null
instead of just string
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because swr/infinite
has some case that cache key is string | null
like
const resolvePageSize = useCallback((): number => {
const cachedPageSize = cache.get(pageSizeCacheKey)
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That feels like a bug (facepalm)
I wonder if there's a way to automatically infer the type, without requiring a generic type to be passed. |
}> = ({ children, value }) => { | ||
type SWRConfigValue = SWRConfiguration & | ||
Partial<ProviderConfiguration> & { | ||
provider?: CacheProvider<any> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not look correct to me.
export type Cache<T = DefaultProvider, Data = any> = T extends ICache<Data>
? T
: never
export type CacheProvider<Data = any> = (cache: Cache<Data>) => Cache<Data>
CacheProvider<any> = (cache: Cache<any>) => Cache<any> = (cache: any) => any
We need provider to return a Cache here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get what do you mean here. Can you rephrase it more specificly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous
type Provider: (cache: Readonly<Cache>) => Cache
const cache = new Map()
<SWRConfig value={{ provider: () => cache }}>{children}</SWRConfig>
// This will throw type error since { a: foo } does not satisfy the requirement
<SWRConfig value={{ provider: () => {a: foo } }}>{children}</SWRConfig>
Current
type Provider = CacheProvider<any> = (_: any) => any
const cache = new Map()
<SWRConfig value={{ provider: () => cache }}>{children}</SWRConfig>
// This will not throw type error any more
<SWRConfig value={{ provider: () => {a: foo } }}>{children}</SWRConfig>
I think the actual problem here is that we can't safely type the cache without runtime checking.
The cache which is returned from the hook probably does not have the clear interface. Currently if users want to extend the cache interface. They have to write a type assertion. // CustomCache.tsx
import type { Cache } from "swr"
import { SWRConfig, useSWRConfig } from "swr"
type MyCustomCache = Map<string, any>
const customCache: MyCustomCache = new Map()
const CustomCacheProvider = ({ children }) => {
return <SWRConfig value={{ provider: () => cache }}>{children}</SWRConfig>
}
// This is just a example. The actual runtime check might be more complex
function checkCache(cache: Cache): cache is MyCustomCache {
return cache === c
}
const useCustomCache = () => {
const { cache } = useSWRConfig()
if (checkCache(cache)) return cache
throw new Error("customCache can only be used within CustomCacheProvider")
} // App.tsx
const ClearButton = () => {
const cache = useCustomCache()
return (
<button
onClick={() => {
// type safe and runtime safe
cache.clear()
}}
></button>
)
}
const App = () => {
return (
<>
<CustomCacheProvider>
<ClearButton />
</CustomCacheProvider>
{/* <ClearButton/>*/}
</>
)
} We need to find a batter way to help user to extend current cache interface (runtime checking is still required) and provider batter type for default cache. |
Cache
a type that extends from required provider interfaceuseSWRConfig
generic with provider typeuseSWRConfig<Provider = Cache>
Closes #1502
Closes #1505