re: React Context with useReducer and Typescript. VIEW POST

re: Thanks! Yes I know if you have more than three reducers the types for the main reducer can increase, and be hard to manage. I don't know how to i...

As it turned out your way of doing the combine reducers also works for type-safe all you gotta do is use type assertion ie:

const mainReducer = ({ products, shoppingCart }: InitialStateType, action: ProductActions | ShoppingCartActions) => ({
  products: productReducer(products, action as ProductActions),
  shoppingCart: shoppingCartReducer(shoppingCart, action as ShoppingCartActions),

By using the 'as' keyword there you can now get rid of the unions type on the action arg of your reducers

Hey, you are right, type assertion works well in this case, it makes the code look cleaner without the union types.

Thanks a lot.

code of conduct - report abuse