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

How to use type transformResponse with enhanceEndpoints in TS. #2244

Open
mattduggan opened this issue Apr 15, 2022 · 1 comment
Open

How to use type transformResponse with enhanceEndpoints in TS. #2244

mattduggan opened this issue Apr 15, 2022 · 1 comment
Labels
bug Something isn't working rtk-query TypeScript

Comments

@mattduggan
Copy link

I'm using the Typescript RTK Query code gen plugin to generate some api hooks.

Example:

const injectedRtkApi = api.injectEndpoints({
  overrideExisting: true,
  endpoints: (build) => ({
    Notification: build.query<NotificationQuery, NotificationQueryVariables>({
      query: (variables) => ({ document: NotificationDocument, variables })
    }),
  }),
});

export { injectedRtkApi as api };
export const { useNotificationQuery, useLazyNotificationQuery } = injectedRtkApi;

I'd like to use enhanceEndpoints to return one of the nested values in the query result:

import {
    api, NotificationQuery,
} from './Notification.generated';

export const {useNotificationQuery} = api.enhanceEndpoints({
    endpoints: {
        Notification: {
            transformResponse: (result: NotificationQuery) => result.notificationByNotificationId
    }
}});

When I do this, TypeScript throws the following error, which I interpret as it expecting transformResponse to be undefined:

TS2322: Type '(result: NotificationQuery) => { __typename?: "Notification" | undefined; id: string; }' is not assignable to type 'undefined'.

I also tried using the definition:

export const {useNotificationQuery} = api.enhanceEndpoints({
    endpoints: {
        Notification(definition) {
            definition.transformResponse: (result: NotificationQuery) => result.notificationByNotificationId
    }
}});

Which is happier but suggests that NotificationQuery['notificationByNotificationId'] is not assignable to NotificationQuery.

Where I use the hook, it also still thinks that data is NotificationQuery and not NotificationQuery['notificationByNotificationId'].

This has my head scratching as there aren't any examples I could find of overriding the ResultType.

I'm using TypeScript 4.4.4 and RTK 1.8.1.

@Hypnosphi
Copy link

Probably the same as #1441

@markerikson markerikson added the bug Something isn't working label Feb 7, 2024 — with Volta.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rtk-query TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants