Given the following
const action1 = (arg1: string) => {}
const action2 = (arg1: string, arg2: {a: string, b: number}) => {}
const actions = [action1, action2]
handleActions(actions)
... elsewhere ...
const handleActions = (actions: WhatTypeIsThis[]) => {
const [action1, action2] = actions;
action1(/** infer string */)
action2(/** infer string and object */)
}
How can I define the WhatTypeIsThis type in order for the action args to be inferable inside handleActions?
Is it possible to define it in such a way that actions can be any number of functions with varying argument lists?
Is it possible using generics?
My solution:
I've marked the accepted answer because it was the inspiration for my solution.
// get the types of the actions, defined in outer scope
type GET = typeof api.get;
type CREATE = typeof api.create;
...
// in controller
handleActions([api.getSomething, api.create])
...
// in service handler
const handleActions = (actions: [GET, CREATE]) => {
const [action1, action2] = actions;
// now we have all input / output type hints
}
This approach lets me isolate my logic from the http server, auth, and everything else I didn't write, so I can test the complexity within my service handlers in peace.
handleActionsfunction? I assume, that your function is more something like this:(actions: DesiredType[], ...params: any) => {}, right?