1

我有一个返回的管道,Either<Error, Task<any>>但我需要的是TaskEither<Error, any>.

我怎样才能转换Either<Error, Task<any>>TaskEither<Error, any>

是否有任何帮助工具功能可以做到这一点?

4

2 回答 2

1

解决方案在这里:

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);
于 2020-01-02T16:24:50.617 回答
0

您可以创建以下转换:

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:我本来想只写chain(rightTask),但是Error用这个速记不能正确推断出类型(现在完全确定为什么)。

尽管如此,对于这些强大的类型来说,这是一个便宜的价格,并提供您想要的结果!

于 2020-01-02T16:17:02.910 回答