I'm learning FP with FP-TS and I just hit a road block:
I have the following function in my repository:
// this is the repository
export const findBook = (id: string) => TaskEither<Error, Option<ParsedBook>>
that part is easy and makes sense to me. The issue is when I try to call it from my controller:
// this is the controller
export const getBook = (req: unknown) => Task<string | ParsedBook>
Anyways, here's my getBook and what I'm trying to do:
const getBook: (req: unknown) => T.Task<string | ParsedBook> = flow(
getBookByIdRequestV.decode, // returns Either<Errors, GetBookByIdRequest>
E.fold(
() => T.of('Bad request!'),
flow(
prop('params'),
prop('id'),
findBook, // returns TaskEither<Error, Option<ParsedBook>>
TE.fold(
() => T.of('Internal error.'),
O.fold(
() => T.of('Book not found.'),
(book) => T.of(book) // this line results in an error
)
)
)
)
)
The issue is that the above code gives me an error:
Type 'ParsedBook' is not assignable to type 'string'
I think the issue is that E.fold expects the result of onLeft and onRight to return the same type:
fold<E, A, B>(onLeft: (e: E) => B, onRight: (a: A) => B): (ma: Either<E, A>) => B
However, it might not only return a Task<string>, but a Task<ParsedBook> as well.
I tried using foldW, which widens the type, but same error as well.
I don't really know what to do; I feel like the way I am modelling the types in my code are bad?
Here's the Codesandbox if it helps: https://codesandbox.io/s/tender-chandrasekhar-5p2xm?file=/src/index.ts
Thank you!