mohsen saremi
mohsen saremi

Reputation: 715

How to convert Either<Error, Task<any>> into TaskEither<Error, any> in fp-ts

I have a pipe that return Either<Error, Task<any>> but what I need is TaskEither<Error, any>.

How can I convert Either<Error, Task<any>> to TaskEither<Error, any>?

Is there any helper any utility function for doing this?

Upvotes: 1

Views: 2180

Answers (2)

mohsen saremi
mohsen saremi

Reputation: 715

The solution is here:

https://github.com/gcanti/fp-ts/issues/1072#issuecomment-570207924

declare const a: E.Either<Error, T.Task<unknown>>;

const b: TE.TaskEither<Error, unknown> = E.either.sequence(T.task)(a);

Upvotes: 1

ford04
ford04

Reputation: 74500

You can create the following conversion:

import { Either } from "fp-ts/lib/Either";
import { Task } from "fp-ts/lib/Task";
import { fromEither, rightTask, chain } from "fp-ts/lib/TaskEither";
import { pipe } from "fp-ts/lib/pipeable";

type MyType = { a: string }; // concrete type instead of any for illustration here

declare const t: Either<Error, Task<MyType>>; // your Either flying around somewhere

const result = pipe(
  fromEither(t), // returns TaskEither<Error, Task<MyType>>
  chain(a => rightTask(a)) // rightTask returns TaskEither<Error, MyType>, chain flattens
); 

// result: TaskEither<Error, MyType>

PS: I would have liked to just write chain(rightTask), but the Error type is not inferred properly with this short-hand (note entirely sure why at the moment).

Nonetheless, that is a cheap price for these strong types and delivers your wanted result!

Upvotes: 1

Related Questions