I'm trying to figure out how to use fp-ts
bind()
function to automatically bind multiple properties from an object to the scope in one go:
import * as A from "fp-ts/Array";
import * as IO from "fp-ts/IO";
const wrap2 = (x: unknown) => () => () => x;
const wrap3 = (x: unknown) => () => () => () => x;
const bindIOEntry = (entry: [string, unknown]) =>
IO.bind(entry[0], wrap3(entry[1]));
const person = {
title: "Mr.",
name: "John",
surname: "Doe",
};
const result = pipe(
IO.Do,
...A.map(bindIOEntry)(Object.entries(person)),
IO.bind("tag", ({ title, name, surname }) =>
wrap2(title() + " " + name() + " " + surname())
)
);
console.log(result().tag()); // Mr. John Doe
The code above works perfectly fine, however, I do get an error on the array destructuring line:
A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)
I've tried it out with simpler pipes & it seems to be a general issue with using destructuring inside a pipe. Is there a solution or any kind of workaround for being able to do multiple binds in one go like this?