swr
swr copied to clipboard
Type safe Key as Url for the SWR fetcher
Bug report
The key type is not type safe as it is evaluated to any
What kind of issues did you encounter with SWR?
I expect the Key to be inferred as the provided value
How did you expect SWR to behave here?
Repro Steps / Code Example
Code exp:
import useSWR from 'swr';
type Data = { id: string };
type Endpoint = '/my-endpoint'; // want type safe endpoints !
const fetcher = (url: Endpoint): Promise<Data> => fetch(url).then((res) => res.json());
export const useTest = () => {
const { data } = useSWR<Data>('/any-thing', (url) => fetcher(url));
};
as shown below the url is inferred as any and this way we could introduce incorrect urls without having compile error.
Additional Context
SWR version: 2.2.5. Add any other context about the problem here.
I want to be able to have something like this :
It could be possible and i was missing something, so it'll be helpful to verify with me. Thanks :)