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

React Query typing explanation #101

Open
MathisBarre opened this issue Oct 26, 2022 · 10 comments
Open

React Query typing explanation #101

MathisBarre opened this issue Oct 26, 2022 · 10 comments

Comments

@MathisBarre
Copy link

MathisBarre commented Oct 26, 2022

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

@diegods-ferreira
Copy link

I really would like that too. I'm now having a really hard time trying to adapt those type declarations to React Query V4 =/

@kbzowski
Copy link

kbzowski commented Dec 5, 2022

+1
Also would be great if I could set onError(error: AxiosError) for QueryConfig. Only onError(error: any) works

@diegods-ferreira
Copy link

diegods-ferreira commented Dec 5, 2022

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]
>;

@tripol
Copy link

tripol commented Jan 2, 2023

Hi @diegods-ferreira Please where does MutationFnType come from?

@alvinvin00
Copy link

Hi @diegods-ferreira Please where does MutationFnType come from?

that's a generic

@diegods-ferreira
Copy link

Hi @diegods-ferreira Please where does MutationFnType come from?

that's a generic

That's right

@tripol
Copy link

tripol commented Jan 10, 2023

Thanks @alvinvin00 @diegods-ferreira

@klevytskyi
Copy link

For those struggling like me. Leaving here another variation of MutationConfig compatible with react-query 4+, typescript 5.1+ and no-args mutation functions support.

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

@galih56
Copy link

galih56 commented Jul 17, 2023

Hi @diegods-ferreira Please where does MutationFnType come from?

that's a generic

What do you mean by generic?

@mateuscqueiros
Copy link

Hi @diegods-ferreira Please where does MutationFnType come from?

that's a generic

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 MutationFnType (which could be any name you want, much like a parameter for functions) is a generic. After that he can use that MutationFnType inside of the type definition, given that we know that MutationFnType is a function (MutationFnType extends Fn).

MutationFnType is called a type variable, because it is capturing and storing a type that will be used later, as we wish.

Documentation for Generics in Typescritpt:
https://www.typescriptlang.org/docs/handbook/2/generics.html

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

8 participants