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

Incorrect types in enhanceEndpoints with transformResponse #3901

Closed
khmilevoi opened this issue Nov 21, 2023 · 1 comment
Closed

Incorrect types in enhanceEndpoints with transformResponse #3901

khmilevoi opened this issue Nov 21, 2023 · 1 comment

Comments

@khmilevoi
Copy link

khmilevoi commented Nov 21, 2023

Codesandbox: https://codesandbox.io/s/redux-toolkit-rtk-query-forked-6plwhy?file=/src/services/pokemon.ts

Somebody had already fixed similar issues, but it was still a problem.

Actual behavior:
When you add transformResponse to enhanceEndpoints, ResultType doesn't change according to transformResponse ReturnType.

Expected behavior:
ResultType should be matched with transformReposponse ReturnType.

Version: 1.9.7

@EskiMojo14
Copy link
Collaborator

see #2953 - the mechanism for overriding resulttype for enhanced endpoints is finicky.

currently you need to do a lot of manual work:

import type {
  DefinitionsFromApi,
  OverrideResultType,
  TagTypesFromApi
} from "@reduxjs/toolkit/dist/query/endpointDefinitions";
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const pokemonApi = createApi({
  baseQuery: fetchBaseQuery({ baseUrl: "https://pokeapi.co/api/v2/" }),
  endpoints: (builder) => ({
    getPokemonByName: builder.query<object, string>({
      query: (name: string) => `pokemon/${name}`
    })
  })
});

type TagTypes = TagTypesFromApi<typeof pokemonApi>;
type Definitions = DefinitionsFromApi<typeof pokemonApi>;

type UpdatedGetPokemonByName = OverrideResultType<
  Definitions["getPokemonByName"],
  number
>;

type FinalDefinitions = Omit<Definitions, "getPokemonByName"> &
  UpdatedGetPokemonByName;

// Export hooks for usage in functional components
export const { useGetPokemonByNameQuery } = pokemonApi.enhanceEndpoints<
  TagTypes,
  FinalDefinitions
>({
  endpoints: {
    getPokemonByName: {
      transformResponse: (): number => 1
    }
  }
});

In order to better support this, we would need to take more drastic (breaking) changes to enhanceEndpoints (see #3485 for what that might look like)

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

2 participants