What I'm trying to do is create a series of nested functions which can be chained together, and the final callback's arguments will be a union of the parents. Example of my failed attempt so far:
export type SomeHandler<T> = (args: T) => void;
type WithFooArgs = { foo: string }
const withFoo = <T,>(handler: SomeHandler<T & WithFooArgs>) => (args: T & WithFooArgs) => handler(args);
type WithBarArgs = { bar: string }
const withBar = <T,>(handler: SomeHandler<T & WithBarArgs>) => (args: T & WithBarArgs) => handler(args);
type WithZooArgs = { zoo: string }
const withZoo = <T,>(handler: SomeHandler<T & WithZooArgs>) => (args: T & WithZooArgs) => handler(args);
export default withFoo(withBar(withZoo((args) => {
// I want args to be type `WithFooArgs & WithBarArgs & WithZooArgs`
})));
The goal is to have a bunch of these which I can chain together in different ways.