-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 fetcher function arguments based on array key (#331) #572
Conversation
Thanks for bringing this up! Do you think if there're any other work arounds that don't require |
I would have thought the
and infers a type of
The I looked into what react-query was doing and noticed https://github.com/tannerlinsley/react-query/blob/master/types/index.d.ts#L202. I don't really understand why, but this apparently forces TypeScript to infer the tuple when the array is passed as the argument directly (not assigned to a variable first). I copied that same type over and it works, except for the case when the key is a function where Note I needed to upgrade TypeScript in order to allow for the circular type reference. |
cc @promer94 would like to hear your thoughts about this |
TypeScript V4 made some great improvements on tuple type inferences. I create a simplified useSWR in typescript playgroud. In this way, user dont have to use 'as const' to force typescript to infer tuples. However, we have to solve some problems before we bring this feature into swr. Stable api designSince we are working on next version fo swr, some of apis may change. Upgrade TypeScript Version
Backward compatibility
In general, i think it would be great if swr could have this ability. I would love to do more researches and experiments on this. |
Any updates on this? |
A solution without |
Changes in tests provide and example of what this looks like:
Need to do
as const
so Typescript infers the tuple type instead of something like(string | object | string[])[]
(in this case). Now, a, b, and c have the proper types and there are type errors if I misuse one of the variables or have the incorrect number of arguments.