Skip to content

Commit 79af5cf

Browse files
authored
Add RawResultType as a type-only property on endpoints (#5037)
* add RawResultType as a type-only property on endpoints * add tests
1 parent af3e75b commit 79af5cf

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,16 @@ export type EndpointDefinitionWithQueryFn<
237237
rawErrorResponseSchema?: never
238238
}
239239

240-
type BaseEndpointTypes<QueryArg, BaseQuery extends BaseQueryFn, ResultType> = {
240+
type BaseEndpointTypes<
241+
QueryArg,
242+
BaseQuery extends BaseQueryFn,
243+
ResultType,
244+
RawResultType,
245+
> = {
241246
QueryArg: QueryArg
242247
BaseQuery: BaseQuery
243248
ResultType: ResultType
249+
RawResultType: RawResultType
244250
}
245251

246252
interface CommonEndpointDefinition<
@@ -519,7 +525,8 @@ type QueryTypes<
519525
TagTypes extends string,
520526
ResultType,
521527
ReducerPath extends string = string,
522-
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
528+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
529+
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType, RawResultType> & {
523530
/**
524531
* The endpoint definition type. To be used with some internal generic types.
525532
* @example
@@ -547,6 +554,7 @@ export interface QueryExtraOptions<
547554
QueryArg,
548555
BaseQuery extends BaseQueryFn,
549556
ReducerPath extends string = string,
557+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
550558
> extends CacheLifecycleQueryExtraOptions<
551559
ResultType,
552560
QueryArg,
@@ -791,7 +799,14 @@ export interface QueryExtraOptions<
791799
/**
792800
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
793801
*/
794-
Types?: QueryTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>
802+
Types?: QueryTypes<
803+
QueryArg,
804+
BaseQuery,
805+
TagTypes,
806+
ResultType,
807+
ReducerPath,
808+
RawResultType
809+
>
795810
}
796811

797812
export type QueryDefinition<
@@ -802,7 +817,14 @@ export type QueryDefinition<
802817
ReducerPath extends string = string,
803818
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
804819
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType, RawResultType> &
805-
QueryExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>
820+
QueryExtraOptions<
821+
TagTypes,
822+
ResultType,
823+
QueryArg,
824+
BaseQuery,
825+
ReducerPath,
826+
RawResultType
827+
>
806828

807829
export type InfiniteQueryTypes<
808830
QueryArg,
@@ -811,7 +833,8 @@ export type InfiniteQueryTypes<
811833
TagTypes extends string,
812834
ResultType,
813835
ReducerPath extends string = string,
814-
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
836+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
837+
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType, RawResultType> & {
815838
/**
816839
* The endpoint definition type. To be used with some internal generic types.
817840
* @example
@@ -838,6 +861,7 @@ export interface InfiniteQueryExtraOptions<
838861
PageParam,
839862
BaseQuery extends BaseQueryFn,
840863
ReducerPath extends string = string,
864+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
841865
> extends CacheLifecycleInfiniteQueryExtraOptions<
842866
InfiniteData<ResultType, PageParam>,
843867
QueryArg,
@@ -987,7 +1011,8 @@ export interface InfiniteQueryExtraOptions<
9871011
BaseQuery,
9881012
TagTypes,
9891013
ResultType,
990-
ReducerPath
1014+
ReducerPath,
1015+
RawResultType
9911016
>
9921017
}
9931018

@@ -1013,7 +1038,8 @@ export type InfiniteQueryDefinition<
10131038
QueryArg,
10141039
PageParam,
10151040
BaseQuery,
1016-
ReducerPath
1041+
ReducerPath,
1042+
RawResultType
10171043
>
10181044

10191045
type MutationTypes<
@@ -1022,7 +1048,8 @@ type MutationTypes<
10221048
TagTypes extends string,
10231049
ResultType,
10241050
ReducerPath extends string = string,
1025-
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
1051+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
1052+
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType, RawResultType> & {
10261053
/**
10271054
* The endpoint definition type. To be used with some internal generic types.
10281055
* @example
@@ -1050,6 +1077,7 @@ export interface MutationExtraOptions<
10501077
QueryArg,
10511078
BaseQuery extends BaseQueryFn,
10521079
ReducerPath extends string = string,
1080+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
10531081
> extends CacheLifecycleMutationExtraOptions<
10541082
ResultType,
10551083
QueryArg,
@@ -1124,7 +1152,14 @@ export interface MutationExtraOptions<
11241152
/**
11251153
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
11261154
*/
1127-
Types?: MutationTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>
1155+
Types?: MutationTypes<
1156+
QueryArg,
1157+
BaseQuery,
1158+
TagTypes,
1159+
ResultType,
1160+
ReducerPath,
1161+
RawResultType
1162+
>
11281163
}
11291164

11301165
export type MutationDefinition<
@@ -1135,7 +1170,14 @@ export type MutationDefinition<
11351170
ReducerPath extends string = string,
11361171
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
11371172
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType, RawResultType> &
1138-
MutationExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>
1173+
MutationExtraOptions<
1174+
TagTypes,
1175+
ResultType,
1176+
QueryArg,
1177+
BaseQuery,
1178+
ReducerPath,
1179+
RawResultType
1180+
>
11391181

11401182
export type EndpointDefinition<
11411183
QueryArg,

packages/toolkit/src/query/tests/createApi.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,23 @@ describe('type tests', () => {
498498
id: number
499499
}>()
500500
expectTypeOf(api.endpoints.query.Types.ResultType).toEqualTypeOf<Post>()
501+
expectTypeOf(api.endpoints.query.Types.RawResultType).toBeAny()
501502

502503
expectTypeOf(api.endpoints.query2.Types.QueryArg).toEqualTypeOf<{
503504
id: number
504505
}>()
505506
expectTypeOf(
506507
api.endpoints.query2.Types.ResultType,
507508
).toEqualTypeOf<Post>()
509+
expectTypeOf(api.endpoints.query2.Types.RawResultType).toBeAny()
508510

509511
expectTypeOf(api.endpoints.query3.Types.QueryArg).toEqualTypeOf<void>()
510512
expectTypeOf(api.endpoints.query3.Types.ResultType).toEqualTypeOf<
511513
EntityState<Post, Post['id']>
512514
>()
515+
expectTypeOf(api.endpoints.query3.Types.RawResultType).toEqualTypeOf<
516+
Post[]
517+
>()
513518
})
514519
})
515520
})

0 commit comments

Comments
 (0)