vercel / swr

React Hooks for Data Fetching
https://swr.vercel.app
MIT License
30.35k stars 1.21k forks source link

feat: modify cache type to allow generic usage #2946

Closed eungwang1 closed 5 months ago

eungwang1 commented 5 months ago

Description:

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');  // data: any

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'); // data:KeyType

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