Skip to content

Commit

Permalink
tweak createSlice logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Feb 5, 2024
1 parent 5f8d2f4 commit e1f3f89
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,14 @@ export const asyncThunkCreator: ReducerCreator<ReducerType.asyncThunk> = {

function noop() {}

const isCreatorCallback = <
State,
CreatorMap extends Record<string, RegisteredReducerType>,
>(
reducers: any,
): reducers is CreatorCallback<State, CreatorMap> =>
typeof reducers === 'function'

interface BuildCreateSliceConfig<
CreatorMap extends Record<string, RegisteredReducerType>,
> {
Expand Down Expand Up @@ -1072,7 +1080,7 @@ export function buildCreateSlice<
getInitialState,
}

if (typeof options.reducers === 'function') {
if (isCreatorCallback(options.reducers)) {
const reducers = options.reducers(creators as any)
for (const [reducerName, reducerDefinition] of Object.entries(reducers)) {
const { _reducerDefinitionType: type } = reducerDefinition
Expand All @@ -1093,7 +1101,7 @@ export function buildCreateSlice<
}
} else {
for (const [reducerName, reducerDefinition] of Object.entries(
options.reducers,
options.reducers as SliceCaseReducers<State>,
)) {
const reducerDetails: ReducerDetails = {
reducerName,
Expand Down

0 comments on commit e1f3f89

Please sign in to comment.