Skip to content
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
62 changes: 52 additions & 10 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,16 @@ export type EndpointDefinitionWithQueryFn<
rawErrorResponseSchema?: never
}

type BaseEndpointTypes<QueryArg, BaseQuery extends BaseQueryFn, ResultType> = {
type BaseEndpointTypes<
QueryArg,
BaseQuery extends BaseQueryFn,
ResultType,
RawResultType,
> = {
QueryArg: QueryArg
BaseQuery: BaseQuery
ResultType: ResultType
RawResultType: RawResultType
}

interface CommonEndpointDefinition<
Expand Down Expand Up @@ -519,7 +525,8 @@ type QueryTypes<
TagTypes extends string,
ResultType,
ReducerPath extends string = string,
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType, RawResultType> & {
/**
* The endpoint definition type. To be used with some internal generic types.
* @example
Expand Down Expand Up @@ -547,6 +554,7 @@ export interface QueryExtraOptions<
QueryArg,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string,
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> extends CacheLifecycleQueryExtraOptions<
ResultType,
QueryArg,
Expand Down Expand Up @@ -791,7 +799,14 @@ export interface QueryExtraOptions<
/**
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
*/
Types?: QueryTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>
Types?: QueryTypes<
QueryArg,
BaseQuery,
TagTypes,
ResultType,
ReducerPath,
RawResultType
>
}

export type QueryDefinition<
Expand All @@ -802,7 +817,14 @@ export type QueryDefinition<
ReducerPath extends string = string,
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType, RawResultType> &
QueryExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>
QueryExtraOptions<
TagTypes,
ResultType,
QueryArg,
BaseQuery,
ReducerPath,
RawResultType
>

export type InfiniteQueryTypes<
QueryArg,
Expand All @@ -811,7 +833,8 @@ export type InfiniteQueryTypes<
TagTypes extends string,
ResultType,
ReducerPath extends string = string,
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType, RawResultType> & {
/**
* The endpoint definition type. To be used with some internal generic types.
* @example
Expand All @@ -838,6 +861,7 @@ export interface InfiniteQueryExtraOptions<
PageParam,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string,
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> extends CacheLifecycleInfiniteQueryExtraOptions<
InfiniteData<ResultType, PageParam>,
QueryArg,
Expand Down Expand Up @@ -987,7 +1011,8 @@ export interface InfiniteQueryExtraOptions<
BaseQuery,
TagTypes,
ResultType,
ReducerPath
ReducerPath,
RawResultType
>
}

Expand All @@ -1013,7 +1038,8 @@ export type InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
ReducerPath
ReducerPath,
RawResultType
>

type MutationTypes<
Expand All @@ -1022,7 +1048,8 @@ type MutationTypes<
TagTypes extends string,
ResultType,
ReducerPath extends string = string,
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType, RawResultType> & {
/**
* The endpoint definition type. To be used with some internal generic types.
* @example
Expand Down Expand Up @@ -1050,6 +1077,7 @@ export interface MutationExtraOptions<
QueryArg,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string,
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> extends CacheLifecycleMutationExtraOptions<
ResultType,
QueryArg,
Expand Down Expand Up @@ -1124,7 +1152,14 @@ export interface MutationExtraOptions<
/**
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
*/
Types?: MutationTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>
Types?: MutationTypes<
QueryArg,
BaseQuery,
TagTypes,
ResultType,
ReducerPath,
RawResultType
>
}

export type MutationDefinition<
Expand All @@ -1135,7 +1170,14 @@ export type MutationDefinition<
ReducerPath extends string = string,
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType, RawResultType> &
MutationExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>
MutationExtraOptions<
TagTypes,
ResultType,
QueryArg,
BaseQuery,
ReducerPath,
RawResultType
>

export type EndpointDefinition<
QueryArg,
Expand Down
5 changes: 5 additions & 0 deletions packages/toolkit/src/query/tests/createApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,23 @@ describe('type tests', () => {
id: number
}>()
expectTypeOf(api.endpoints.query.Types.ResultType).toEqualTypeOf<Post>()
expectTypeOf(api.endpoints.query.Types.RawResultType).toBeAny()

expectTypeOf(api.endpoints.query2.Types.QueryArg).toEqualTypeOf<{
id: number
}>()
expectTypeOf(
api.endpoints.query2.Types.ResultType,
).toEqualTypeOf<Post>()
expectTypeOf(api.endpoints.query2.Types.RawResultType).toBeAny()

expectTypeOf(api.endpoints.query3.Types.QueryArg).toEqualTypeOf<void>()
expectTypeOf(api.endpoints.query3.Types.ResultType).toEqualTypeOf<
EntityState<Post, Post['id']>
>()
expectTypeOf(api.endpoints.query3.Types.RawResultType).toEqualTypeOf<
Post[]
>()
})
})
})
Expand Down
Loading