typescript-fsa-reducers
typescript-fsa-reducers copied to clipboard
Add the ability to .case on a string
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?
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.
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.