query icon indicating copy to clipboard operation
query copied to clipboard

QueryClientConfig is missing type args

Open aryzing opened this issue 2 years ago • 3 comments

Describe the bug

The type QueryClientConfig is missing type args. It is currently defined as

export interface QueryClientConfig {
    queryCache?: QueryCache;
    mutationCache?: MutationCache;
    defaultOptions?: DefaultOptions;
}

and used in the QueryClient constructor as

constructor(config: QueryClientConfig = {}) // ...

This makes it impossible to define a typed config object with types other than unknown.

Your minimal, reproducible example

Already in bug description

Steps to reproduce

In bug description

Expected behavior

Ability to create a typed config with types other than unknown used it's porpertyes type args.

const defaultOptions: DefaultOptions<ApiError> = {/* ... */};
const queryClient = new QueryClient({ defaultOptions });

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

N/A

react-query version

"react-query": "3.34.16",

TypeScript version

"typescript": "4.6.2",

Additional context

No response

aryzing avatar Mar 02 '22 09:03 aryzing

Related to this is the fact that when creating a defaultOptions object, there's a type error for the mutationFn prop,

const defaultOptions: DefaultOptions = {};

const queries: QueryObserverOptions = {};
const mutations: MutationObserverOptions = {};


queries.onError = (error) => {/*...*/};
mutations.onError = (error) => {/*...*/};


defaultOptions.queries = queries;
defaultOptions.mutations = mutations;
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Types of property 'mutationFn' are incompatible.
    Type 'MutationFunction<unknown, void> | undefined' is not assignable to type 'MutationFunction<unknown, unknown> | undefined'.
      Type 'MutationFunction<unknown, void>' is not assignable to type 'MutationFunction<unknown, unknown>'.
        Type 'unknown' is not assignable to type 'void'.

42 defaultOptions.mutations = mutations;
   ~~~~~~~~~~~~~~~~~~~~~~~~
*/

const queryClient = new QueryClient({ defaultOptions });

aryzing avatar Mar 02 '22 17:03 aryzing

Are they worried that we could potentially use a different error type on some queries? My use case is openapi gen client so we internally guarantee an error type of ApiError. I feel like we should be given the option to define this if we need

Alcas1 avatar Jul 28 '22 20:07 Alcas1

can someone please provide a reproduction in https://typescriptlang.org/play ? From the description alone, I don't understand the problem fully.

TkDodo avatar Jul 29 '22 15:07 TkDodo

@aryzing Looks like you wish QueryClientConfig/DefaultOptions types to be generic and to take type args because error type is predictable? If I'm understanding you correctly, this discussion has talked about it already.

SevenOutman avatar Sep 07 '22 03:09 SevenOutman

The discussion proved useful, will close.

aryzing avatar Sep 09 '22 15:09 aryzing