Skip to content

Conversation

@kachkaev
Copy link
Contributor

@kachkaev kachkaev commented Apr 2, 2021

This PR fixes the following edge case issue in typings:

import { QueryFunction, useQuery } from "react-query";

type MyData = number;
type MyQueryKey = readonly ["my-data", number];

const getMyData: QueryFunction<MyData, MyQueryKey> = async ({
  queryKey: [, n],
}) => {
  return n + 42;
};

// ...

const myData = useQuery({
  queryKey: ["my-data", 100],
  queryFn: getMyData,
});

Before

No overload matches this call.
  Overload 1 of 3, '(options: UseQueryOptions<number, unknown, number, (string | number)[]>): UseQueryResult<number, unknown>', gave the following error.
    Type 'QueryFunction<number, MyQueryKey>' is not assignable to type 'QueryFunction<number, QueryKey>'.
      Type 'QueryKey' is not assignable to type 'MyQueryKey'.
        Type 'string' is not assignable to type 'readonly ["my-data", number]'.
  Overload 2 of 3, '(queryKey: QueryKey, options?: UseQueryOptions<unknown, unknown, unknown, QueryKey> | undefined): UseQueryResult<unknown, unknown>', gave the following error.
    Argument of type '{ queryKey: (string | number)[]; queryFn: QueryFunction<number, MyQueryKey>; }' is not assignable to parameter of type 'QueryKey'.
      Object literal may only specify known properties, and 'queryKey' does not exist in type 'readonly unknown[]'.ts(2769)
types.d.ts(32, 5): The expected type comes from property 'queryFn' which is declared here on type 'UseQueryOptions<number, unknown, number, (string | number)[]>'

After

No TS error

@vercel
Copy link

vercel bot commented Apr 2, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/tannerlinsley/react-query/CNZLrr5jSyvghaYiruoHa1K6Wzy1
✅ Preview: https://react-query-git-fork-kachkaev-patch-1-tannerlinsley.vercel.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 2, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 415e4a4:

Sandbox Source
tannerlinsley/react-query: basic Configuration
tannerlinsley/react-query: basic-typescript Configuration

@kachkaev kachkaev marked this pull request as ready for review April 2, 2021 16:06
@kachkaev kachkaev changed the title Add TQueryKey to QueryFunction generic type in QueryOptions Add TQueryKey to QueryFunction generic type in QueryOptions, remove ensureArray from Query Apr 3, 2021
@TkDodo TkDodo merged commit c343457 into TanStack:master Apr 17, 2021
@tannerlinsley
Copy link
Member

🎉 This PR is included in version 3.13.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants