cartant/ts-action

Feature request: ability to pass dynamic number of arguments into union

aarjithn opened this issue · 3 comments

Hi @cartant,

Opening a new issue as you said as the question here is not regarding limited number of arguments.

Currently, union accepts a list of arguments as actions. If we have a case where all the actions are held in an object (say via import *, the action names need to be duplicated before passing onto union.

// actions.ts
export const action1 = action('ACTION1'..),
export const action2 = action('ACTION2'..),

//reducers.ts
import * as actions from './actions'
const all = union(actions.action1, actions.action2...)

However, if we add/remove actions, this argument list needs to be updated too which is not very DRY.

Now, i tried const all = union.apply(null, Object.values(actions));, but that doesn't work and all is getting type any.

if you think this is a valid usecase and could be handled easily, request you to add support for the same, else this issue can be closed.

Thank you for your work on the library.

You should be able to convert the object to an array and then spread it into the union call, like this union(...array). Have a look at this example:

https://www.typescriptlang.org/play/?ssl=7&ssc=2&pln=1&pc=1#code/MYewdgzgLgBCBGArApsWBeGBvGYBcMALAEwA0MEBARAKyFUwC+A3AFCiSwCGATj1wE8YmAGY8QAWwBqXADYBXZBAAUCFGgCUbViPlg0AS3AwxkmQqUAeACoA+ZT1QgeAEwIAlJ68vQeBsADm5HYaBNYA2gC62KwwcTAc0DAA1sgCEMIwAPJIqFAAdKnpDl4uWrHxjlDyPGApaRD5ElwADspFwrYwjqCu4UWR5YxAA

I'll consider adding a helper - or an overload - to the package to do this, but you should be able to do this yourself, in the interim.

I don't think I'll add anything for this use case. I don't think it's necessary, as Object.values should do what you need:

const all = union(...Object.values(actions))

yes it works. Thanks a lot!