I'm attempting to use functional composition to add behaviors to an object with mixins:
const pipe = (...funcs: ((...args: any[]) => any)[]) => (initial: any) => funcs.reduce((object, fn) => fn(object), initial);
const speakMixin = <T,>(obj: T): T & { speak: () => void } => ({
...obj,
speak: () => console.log("I can speak!")
});
const flyMixin = <T,>(obj: T): T & { fly: () => void } => ({
...obj,
fly: () => console.log("i'm flying")
});
const chain = pipe(speakMixin, flyMixin);
const mixed = chain({});
mixed.fly(); // fly member is typed to any
Is there a better way to type my pipe
function so that I get type safety on objects to which I have applied mixins?