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

Fix TransformedResponse type to unwrap promise #3500

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
OmitFromUnion,
CastAny,
NonUndefined,
UnwrapPromise,
} from './tsHelpers'
import type { NEVER } from './fakeBaseQuery'
import type { Api } from '@reduxjs/toolkit/query'
Expand Down Expand Up @@ -797,7 +798,9 @@ export type TransformedResponse<
> = K extends keyof NewDefinitions
? NewDefinitions[K]['transformResponse'] extends undefined
? ResultType
: ReturnType<NonUndefined<NewDefinitions[K]['transformResponse']>>
: UnwrapPromise<
ReturnType<NonUndefined<NewDefinitions[K]['transformResponse']>>
>
: ResultType

export type OverrideResultType<Definition, NewResultType> =
Expand Down
7 changes: 2 additions & 5 deletions packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,16 +581,13 @@ describe('endpoint definition typings', () => {
enhancedApi.endpoints.query1.initiate()
)
expect(queryResponse.data).toEqual({ value: 'transformed' })
expectType<Transformed | Promise<Transformed> | undefined>(
queryResponse.data
)
expectType<Transformed | undefined>(queryResponse.data)

const mutationResponse = await storeRef.store.dispatch(
enhancedApi.endpoints.mutation1.initiate()
)
expectType<
| { data: Transformed | Promise<Transformed> }
| { error: FetchBaseQueryError | SerializedError }
{ data: Transformed } | { error: FetchBaseQueryError | SerializedError }
>(mutationResponse)
expect('data' in mutationResponse && mutationResponse.data).toEqual({
value: 'transformed',
Expand Down