How to merge object methods by composing them? #31
-
Hi, first of all, thanks for your hard work with this library and the readme for it. function inspectionFormMergeCustomizer(objValue, srcValue, key) {
if (
typeof objValue === 'function' &&
typeof srcValue === 'function'
) {
return (transformed, ...args) => {
return objValue(srcValue(transformed, ...args), ...args);
};
}
} the problem with lodash mergeWith is that I lost all type information. I was looking at this library and it seems it is possible to do that, but my TS skill is still very low and cannot think on how to do it, Is it possible to do something similar and still infer the types from the resulting composed function? Thanks in advance for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Here's how you would customize deepmerge-ts to compose the functions: import { deepmergeCustom } from "deepmerge-ts";
export const myDeepmerge = deepmergeCustom({
mergeOthers: (values, utils) => {
if (areAllFunctions(values)) {
return composeFunctions(values);
}
return utils.defaultMergeFunctions.mergeOthers(values, utils);
},
});
function areAllFunctions(
values: readonly unknown[]
): values is ReadonlyArray<(...args: unknown[]) => unknown> {
return values.every((value) => typeof value === "function");
}
function composeFunctions(fns: ReadonlyArray<(...args: unknown[]) => unknown>) {
return (param: unknown) =>
fns.reduceRight((accumulator, fn) => fn(accumulator), param);
} However this won't necessarily have the right type information for the composed function. Could you give me an example of a function you'd like to compose? Then I can adjust the code above to give the right type info. |
Beta Was this translation helpful? Give feedback.
Here's how you would customize deepmerge-ts to compose the functions: