-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
React Query typing explanation #101
Comments
I really would like that too. I'm now having a really hard time trying to adapt those type declarations to React Query V4 =/ |
+1 |
For anyone who would like to know, I was able to adapt the MutationConfig type to work with React Query v4. It is like this now: import { AxiosError } from 'axios';
import { QueryClient, UseQueryOptions, UseMutationOptions, DefaultOptions } from '@tanstack/react-query';
import { AsyncReturnType } from 'type-fest';
...
type Fn = (...args: any) => any;
export type MutationConfig<MutationFnType extends Fn> = UseMutationOptions<
ExtractFnReturnType<AsyncReturnType<MutationFnType>>,
AxiosError,
Parameters<MutationFnType>[0]
>; |
Hi @diegods-ferreira Please where does |
that's a generic |
That's right |
Thanks @alvinvin00 @diegods-ferreira |
For those struggling like me. Leaving here another variation of type Fn = (...args: any[]) => any;
type GetTVariables<T> = T extends (...args: infer R) => any ? (R extends [infer TVar, ...any] ? TVar : void) : never;
export type MutationConfig<MutationFnType extends Fn> = UseMutationOptions<
ExtractFnReturnType<Awaited<ReturnType<MutationFnType>>>,
AxiosError,
GetTVariables<MutationFnType>
>; |
What do you mean by generic? |
A generic is a special notation in typescript that allows you to use a variety of types within the same definition. Think of it as parameters for functions, but instead of passing values, we are a passing a type itself. In the example, the code inside the <> called
Documentation for Generics in Typescritpt: |
As you can see here, this project use additional types for react query.
I honestly find it hard to understand what they are doing.
It would be great if someone could add some explanations
The text was updated successfully, but these errors were encountered: