weixin_39636850
weixin_39636850
2020-12-09 08:10

More than 3 actions in reducer's on statement.

I stacked with the problem that reducer is not doing autoresolve of handler arguments types if i will send there more than 3 action in one queue.

image image

I also found hardcoded that you accepts only 3 args: export declare function on<C1 extends ActionCreator, C2 extends ActionCreator, C3 extends ActionCreator, S>(creator1: C1, creator2: C2, creator3: C3, reducer: OnReducer<S, [C1, C2, C3]>): On<S>; Is this possible to make it more generic?

I really like how this library helps solving redux boilerplate code problem and i want continue using it.

该提问来源于开源项目:cartant/ts-action

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • weixin_39922147 weixin_39922147 5月前

    I've pushed the changes. If you are interested in the usage, have a look at this test:

    https://github.com/cartant/ts-action/blob/f3647a763bac6425fa2ebdfee451fcbe2cb6cab9/packages/ts-action/source/reducer-spec.ts#L94-L116

    If you are interested in the TypeScript behaviours that need to be worked around, have a look at these experiments:

    https://github.com/cartant/ts-action/blob/master/packages/ts-action/source/reducer-experiment-spec.ts

    I've make some changes to the README and will publish a new version later.

    It would be simpler to just use an array of action creators - rather than a rest parameter - but mixing array and non-array signatures is something I wanted to avoid. And, who knows, maybe the TypeScript behaviour might change, some day, to better suit this.

    Another alternative could have been to use an API something like this:

    ts
      on(foo, bar, baz, boo).reduce((state, action) => state)
    

    Doing so would have made things much simpler.

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    v11.0.0 has been published and you can now use union to pass as many action creators as you like to on: https://github.com/cartant/ts-action/blob/master/packages/ts-action/README.md#reducer

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    Yeah. I can do something about this. It's the way it is because I simply could no get TypeScript to do what I wanted. I can see, now, what the problem is and that it's not something in TypeScript that is ever going to change.

    I don't just want to keep adding overload signatures. That's what's done in NgRx's createReducer - which is based on ts-action. It has ten overload signatures. I would like a proper solution to this.

    I have a few ideas for a solution. And I have a question that's related: have you ever had cause to use the union function that's in this package? I'm considering tweaking it to use in the solution. It would be an easily-fixed breaking change - for users of union - and I'm wondering whether people use it often. I suppose that if you are using the reducer function union is not something you really need?

    点赞 评论 复制链接分享
  • weixin_39636850 weixin_39636850 5月前

    Thanks a lot for the quick reply. I know about this typescript problem too. And i think that helper function that helps us to combine many action will be great solution. Shoult it be something like that? image

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    Yeah. Close. It will look like this:

    
    on(...union(foo, bar), (state, action) => {})
    

    The fundamental problem is that TypeScript will not infer a union of functions and the action creators - foo and bar - are functions. I will change the implementation of union so that it returns an array of action creators with the types changed so that they are not functions - the implementation of on will be the same. There will just be some type trickery.

    Having heaps of overload signatures is something I really don't like. I think this will be a reasonable solution.

    I will also change the current catch-all signature so that it will work without error. It will just lose the narrowing.

    点赞 评论 复制链接分享

相关推荐