Skip to content
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

Type safe Key as Url for the SWR fetcher #2927

Open
ItsTarik opened this issue Mar 30, 2024 · 0 comments
Open

Type safe Key as Url for the SWR fetcher #2927

ItsTarik opened this issue Mar 30, 2024 · 0 comments

Comments

@ItsTarik
Copy link

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

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

No branches or pull requests

1 participant