swr icon indicating copy to clipboard operation
swr copied to clipboard

Type safe Key as Url for the SWR fetcher

Open ItsTarik opened this issue 2 months ago • 0 comments

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));
};

image 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 : image

It could be possible and i was missing something, so it'll be helpful to verify with me. Thanks :)

ItsTarik avatar Mar 30 '24 22:03 ItsTarik