-
A friend of mine made a function for deleting multiples values from object but types are not working. I need a help to make types work. import { pipe } from 'fp-ts/lib/function';
import { map } from 'fp-ts/Array';
import { deleteAt } from 'fp-ts/Record';
const person = { name: 'Ammar', email: '[email protected]', dob: '12/12/2000' };
const surround = map(deleteAt);
const omit = (arr: string[], obj: object) => pipe(obj, ...surround(arr));
console.log(omit(['name', 'dob'], person)); and the current error while passing the surround function in pipe and using spread operator is;
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Your code does not work because To reflect a loop in functional programming recursion is preferred. In your example you would need to use import { pipe } from "fp-ts/lib/function";
import { map, reduce } from "fp-ts/Array";
import { deleteAt } from "fp-ts/Record";
// ...
const omit = (arr: string[], obj: object) =>
pipe(
arr,
surround,
reduce(obj, (reducedObj, f) => f(reducedObj))
); However, this is just a quick idea how to fix the error. The output type is still just Please take a look at the import { omit } from "fp-ts-std/Struct";
const reducedPerson = pipe(person, omit(["name", "dob"])) |
Beta Was this translation helpful? Give feedback.
Your code does not work because
pipe
does not support rest parameter for arrays. It is not "passed to a rest". In other wordspipe
is unfortunately not generic for an arbitrary number of parameters. It supports only max 20 parameters and that's why it is saying "must [...] have a tuple type".To reflect a loop in functional programming recursion is preferred. In your example you would need to use
reduce
. For exampleHowe…