typescript-fsa-reducers icon indicating copy to clipboard operation
typescript-fsa-reducers copied to clipboard

Add the ability to .case on a string

Open MJLHThomassen-Eurocom opened this issue 6 years ago • 2 comments

I'm currently working with react-router-redux which defines a LOCATION_CHANGE, however since the library is not using typescript-fsa-reducers, i can't .case() on that action in a reducer.

I want to clear an error message string on every LOCATION_CHANGE action. As a workaround, I have created a "new" actionCreator(LOCATION_CHANGE) in my list of actions for the control this concerns, however, perhaps there is a way to also allow external actions in a .case statement? This would obviously mean loosing type safety. However, react-router-redux does define the propper LocationChangeAction action interface, so perhaps this can be solved with generics?

MJLHThomassen-Eurocom avatar Mar 05 '18 12:03 MJLHThomassen-Eurocom

Ah yes, this makes sense as a use case. As you said it does lose type safety, but that can't really be helped. I don't know if generics are particularly helpful because

.case<FooAction>("FOO", (state, action) => { /*...*/ })

isn't any safer than

.case("FOO", (state, action: FooAction) => { /*...*/ })

but it's probably fine to be untyped when dealing with untyped external libraries. In my own code I would probably create the actionCreator(LOCATION_CHANGE) like you described, but I can see the value in making it possible to skip this step as well.

I'll try to make a change for this some time this week.

dphilipson avatar Mar 05 '18 22:03 dphilipson

Thanks!

You are right about both examples you give being equally typesafe, but it would be desirable to be able to do it (e.g. not restrict action to AnyAction but to T extends AnyAction) obviously.

MJLHThomassen-Eurocom avatar Mar 06 '18 07:03 MJLHThomassen-Eurocom