0

Say we have a SharedReducer with the state being SharedState

We also have...

AReducer with the state being SharedState & AState

BReducer with the state being SharedState & BState

CReducer with the state being SharedState & CState

The overall state of the combined reducer should be

{ A: {}, B: {}, C: {}, Shared: {}}

A Reducer would have access to A and Shared

B Reducer would have access to B and Shared

C Reducer would have access to C and Shared

I want to create a typed combine reducer function that can handle the combination

this is what I have but the newly defined state with newState?.[eventType] is throwing errors

type Reducer<K extends keyof RootState> = (
  state: DebugState & Pick<RootState, K>,
  action: any
) => DebugState & Pick<RootState, K>

type GenericReducer<K extends keyof RootState> = {
  eventType: K
  reducer: Reducer<K>
}

function combineReducers(...reducers: GenericReducer<'DEBUG' | 'EVENT'>[]) {
  return (state: RootState, action: any) => {
    return reducers.reduce((newState: RootState, { eventType, reducer }) => {
      return {
        ...newState,
        ...reducer(
          {
            [debugType]: newState[debugType],
            ...(newState?.[eventType] ?? undefined),
          },
          action
        ),
      }
    }, state)
  }
}
mfs780
  • 9
  • 2

0 Answers0