Skip to content

"Types of property 'isUninitialized' are incompatible." when extending a useQuery hook #2660

Open
@denchen

Description

@denchen

I'm trying to extend a useQuery() hook with the newly released TypedUseQueryHookResult type introduced in 1.8.4, but I'm getting the following:

Types of property 'isUninitialized' are incompatible.
  Type 'true' is not assignable to type 'false'.ts(2322)

The code (with CodeSandbox):

import {
  createApi,
  fetchBaseQuery,
  TypedUseQueryHookResult
} from "@reduxjs/toolkit/query/react";

type MyApiProps = {
  id: string;
};

type MyApiResponse = Array<{
  id: string;
  name: string;
}>;

export const api = createApi({
  reducerPath: "myApi",
  baseQuery: fetchBaseQuery({ baseUrl: "https://some-api.com/api/v1" }),
  tagTypes: ["MyApi"],
  endpoints: (build) => ({
    getData: build.query<MyApiResponse, MyApiProps>({
      query: ({ id }) => `my-api/${id}`
    })
  })
});

export const useGetData = (
  args: MyApiProps
): TypedUseQueryHookResult<
  MyApiResponse,
  MyApiProps,
  ReturnType<typeof fetchBaseQuery>
> =>
  api.useGetDataQuery(args, {
    selectFromResult: (results) => ({
      ...results,
      data: results.data?.filter((d) => d.name === "Harry")
    })
  });

Note that this usage of selectFromResult() works just fine if I use the auto-generated hook directly in a component.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions