Skip to content

transformResponse in enhanceEndpoints returning Promise type when trying to transform the type of the response #3443

Closed
@cini9

Description

@cini9

Following the update to V1.9.4, with the ability to override TS type of the original data in transformResponse of enhanceEndpoints, the response type I am receiving in my components are being additionally transformed to add a Promise<> type of my desired response type.

I am using codegen to generate types from my backend, but need to transform that response to extract lower-level data from the raw graphql query response.

type TransformedGetUser = GetUserQuery['getUser']
type TransformedGetUsers = GetUsersQuery['getUsers']

type Definitions = DefinitionsFromApi<typeof generatedUserApi>
type TagTypes = TagTypesFromApi<typeof generatedUserApi>

type UpdatedDefinitions = Omit<Definitions, 'getUsers' | 'getUser'> & {
  getUsers: OverrideResultType<Definitions['getUsers'], TransformedGetUsers>
  getUser: OverrideResultType<Definitions['getUser'], TransformedGetUser>
}

const userApi = generatedUserApi.enhanceEndpoints<TagTypes, UpdatedDefinitions>(
  {
    endpoints: {
      getUsers: {
        providesTags: ['User'],
        transformResponse: (response: GetUsersQuery): TransformedGetUsers =>
          response.getUsers,
      },
      getUser: {
        providesTags: ['User'],
        transformResponse: (response: GetUserQuery): TransformedGetUser =>
          response.getUser,
      },
    },
  }
)

This gives me the typing in the components in the screen shot below, with my correct type (id: string, firstName: string, etc) followed by the Promise<...> type.

image

When I try to use the data returned from these queries, I'm getting a 'property id does not exist on type Promise< .... >'

image

Is there any way to tell RTK to not add the Promise type when we are overriding the response type in transformResponse ?

Thanks in advance for your help !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions