Skip to content

#2047 may have introduced a Typescript bug for queries generics #2115

@lukemorales

Description

@lukemorales

Describe the bug
Before #2047 being merged, I used to type useQuery with just useQuery<DataType, ErrorType> to cast the error from axios. After the merge, all my typed hooks broke because if I don't type all the 4 type parameters, Typescript is inferring the second parameter as the options object instead of the function, thus throwing a Type error, as shown by the screenshot[1].

It also broke the way I typed the queries object for useQueries, as I need now to type all 4 type parameters of each array object. It was easy and very handy to use the UseQueryOptions type for the auto complete, but now it's pretty unusable if I don't type all my queries types, and if it's a lot of objects with different types, it get's very verbose (screenshot[2]).

To Reproduce
Steps to reproduce the behavior:

  1. Install latest version.
  2. Only provide the two first parameters to useQuery generic and Typescript will complain about the query function not being typed correctly.
  3. Install react-query version 3.12.2
  4. Typescript will stop complaining.

Expected behavior
Typescript should infer the second parameter of useQuery as the query function even if only two parameters are provided to the type generics, but a different issue happens with the useQueryOptions, so I think the QueryKey inferring introduced by #2047 may have brought a more in depth issue.

Screenshots

Screen Shot 2021-04-13 at 01 10 03

Screen Shot 2021-04-13 at 01 30 26

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions