Skip to content

feat: modify cache type to allow generic usage#2947

Merged
shuding merged 1 commit intovercel:mainfrom
eungwang1:main
Jul 19, 2025
Merged

feat: modify cache type to allow generic usage#2947
shuding merged 1 commit intovercel:mainfrom
eungwang1:main

Conversation

@eungwang1
Copy link
Copy Markdown
Contributor

Current Issue:
The current implementation of cache.get does not support explicit type definitions, which limits type safety and can lead to potential issues with type inference. Here's how it's currently implemented:

const { cache } = useSWRConfig();
const data = cache.get('key'); 

Proposed Changes:
This pull request modifies cache.get to accept generic type parameters, enhancing type safety by allowing developers to specify the expected return type. The modified implementation is as follows:

const { cache } = useSWRConfig();
const data = cache.get<KeyType>('key');

This update ensures better type checking and integration with TypeScript projects, aligning with modern development practices in type-safe applications.

@codesandbox-ci
Copy link
Copy Markdown

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.

@shuding shuding merged commit 063fe55 into vercel:main Jul 19, 2025
@huozhi
Copy link
Copy Markdown
Member

huozhi commented Jul 29, 2025

This change breaks the provider prop's type of SWRConfig, it won't work with Map primitives

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants