From 88fa75f05481ed07da873bc804f6ae750f4138b1 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Tue, 3 Sep 2024 10:19:03 +0100 Subject: [PATCH] avoid declare module issue --- packages/toolkit/src/asyncThunkCreator.ts | 68 ++++++++++--------- packages/toolkit/src/createSlice.ts | 13 ++-- .../toolkit/src/entities/slice_creator.ts | 14 +--- 3 files changed, 44 insertions(+), 51 deletions(-) diff --git a/packages/toolkit/src/asyncThunkCreator.ts b/packages/toolkit/src/asyncThunkCreator.ts index a2a974a1bf..b7e9ec7cb7 100644 --- a/packages/toolkit/src/asyncThunkCreator.ts +++ b/packages/toolkit/src/asyncThunkCreator.ts @@ -10,42 +10,12 @@ import type { CaseReducer } from './createReducer' import type { CreatorCaseReducers, ReducerCreator, + ReducerCreatorEntry, ReducerDefinition, } from './createSlice' import { ReducerType } from './createSlice' import type { Id } from './tsHelpers' -export type AsyncThunkCreatorExposes< - State, - CaseReducers extends CreatorCaseReducers, -> = { - actions: { - [ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition< - State, - infer ThunkArg, - infer Returned, - infer ThunkApiConfig - > - ? AsyncThunk - : never - } - caseReducers: { - [ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition< - State, - any, - any, - any - > - ? Id< - Pick< - Required, - 'fulfilled' | 'rejected' | 'pending' | 'settled' - > - > - : never - } -} - export type AsyncThunkSliceReducerConfig< State, ThunkArg extends any, @@ -145,6 +115,42 @@ export interface AsyncThunkCreator< > } +export type AsyncThunkCreators< + State, + CaseReducers extends CreatorCaseReducers, +> = { + [ReducerType.asyncThunk]: ReducerCreatorEntry< + AsyncThunkCreator, + { + actions: { + [ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition< + State, + infer ThunkArg, + infer Returned, + infer ThunkApiConfig + > + ? AsyncThunk + : never + } + caseReducers: { + [ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition< + State, + any, + any, + any + > + ? Id< + Pick< + Required, + 'fulfilled' | 'rejected' | 'pending' | 'settled' + > + > + : never + } + } + > +} + export const asyncThunkCreator: ReducerCreator = { type: ReducerType.asyncThunk, create: /* @__PURE__ */ (() => { diff --git a/packages/toolkit/src/createSlice.ts b/packages/toolkit/src/createSlice.ts index 76dfeeb730..dcf3ee98a3 100644 --- a/packages/toolkit/src/createSlice.ts +++ b/packages/toolkit/src/createSlice.ts @@ -1,9 +1,6 @@ import type { Action, Reducer, UnknownAction } from 'redux' import type { Selector } from 'reselect' -import type { - AsyncThunkCreator, - AsyncThunkCreatorExposes, -} from './asyncThunkCreator' +import type { AsyncThunkCreators } from './asyncThunkCreator' import type { InjectConfig } from './combineSlices' import type { ActionCreatorWithoutPayload, @@ -20,6 +17,7 @@ import type { ReducerWithInitialState, } from './createReducer' import { createReducer, makeGetInitialState } from './createReducer' +import type { EntityCreators } from './entities/slice_creator' import type { ActionReducerMapBuilder, TypedActionCreator } from './mapBuilders' import { executeReducerBuilderCallback } from './mapBuilders' import type { @@ -76,7 +74,8 @@ export interface SliceReducerCreators< CaseReducers extends CreatorCaseReducers, Name extends string, ReducerPath extends string, -> { +> extends AsyncThunkCreators, + EntityCreators { [ReducerType.reducer]: ReducerCreatorEntry< { ( @@ -142,10 +141,6 @@ export interface SliceReducerCreators< } } > - [ReducerType.asyncThunk]: ReducerCreatorEntry< - AsyncThunkCreator, - AsyncThunkCreatorExposes - > } export type ReducerCreators< diff --git a/packages/toolkit/src/entities/slice_creator.ts b/packages/toolkit/src/entities/slice_creator.ts index 0b20111c03..b1fbe29903 100644 --- a/packages/toolkit/src/entities/slice_creator.ts +++ b/packages/toolkit/src/entities/slice_creator.ts @@ -1,6 +1,5 @@ import type { CaseReducerDefinition, - CreatorCaseReducers, PayloadAction, ReducerCreator, ReducerCreatorEntry, @@ -30,7 +29,7 @@ export interface EntityMethodsCreatorConfig< pluralName?: Plural } -type EntityMethodsCreator = +export type EntityMethodsCreator = State extends EntityState ? { < @@ -69,15 +68,8 @@ type EntityMethodsCreator = >, ) => EntityReducers -declare module '@reduxjs/toolkit' { - export interface SliceReducerCreators< - State, - CaseReducers extends CreatorCaseReducers, - Name extends string, - ReducerPath extends string, - > { - [entityMethodsCreatorType]: ReducerCreatorEntry> - } +export type EntityCreators = { + [entityMethodsCreatorType]: ReducerCreatorEntry> } const makeWrappedReducerCreator =