【发布时间】:2021-07-30 05:01:25
【问题描述】:
当我遇到障碍时,我正试图在 fp-ts 中编写 localStorage 包装器。我想处理null 的值以及localStorage 抛出的异常,所以我从这段代码开始:
import * as IOE from "fp-ts/IOEither";
import * as O from "fp-ts/Option";
const getItem = (key: string): IOE.IOEither<Error, O.Option<string>> =>
IOE.tryCatch(
() => O.fromNullable(localStorage.getItem(key)),
E.toError
)
上面的函数有一个返回签名IOEither<Error, Option<string>>。我想将Option 合并到IOEither 中,即得到一个IOEither<Error, string>。我将如何实现这一目标?
附:我想上述问题也与TaskEither<Error, Option<string>> 的情况有关。
【问题讨论】:
标签: typescript functional-programming monads fp-ts