lagunovsky / redux-react-router

A Redux binding for React Router v6
MIT License
72 stars 9 forks source link

Fix `UpdateLocationActions` type #17

Closed micha149 closed 1 year ago

micha149 commented 1 year ago

Previously the type was an intersection of all methods which typescript unexpectedly narrows to a single forward action. But instead of an intersection we actually want a union to describe one of the actions instead all the actions.

This problem occurred as I used Observable<RouterActions> as the return type of an redux-observable epic.

Fixing the type caused problems on another unexpected intersection type for the action in the middleware. Fixing this one too caused typescript to be not able to narrow the argument types for the history method anymore. To help typescript here I added a switch block which handles each tuple type separately. A bit more code than before but I saw no other solution.

I also added to type guards matchLocationChangeAction and matchUpdateLocationActions which I used to match the actions and help typescript to determine the correct types. I also exported them to be able to use them in user code with @reduxjs/toolkit or redux-observable.

lagunovsky commented 1 year ago

Thank you.

P.S. I've made minor edits.