How to turn a TaskOption into a TaskEither using fp-ts?

Viewed 140

I have a function that can be used to find an object in the database and returns a TaskOption:

find: (key: SchemaInfo) => TO.TaskOption<Schema>

and another one that saves it:

register: (schema: Schema) => TE.TaskEither<Error, void>

In my register implementation I'd like to check whether the Schema I'm trying to save exists or not (I want to use find), but I don't know how to map a TaskOption into a TaskEither. I tried to do this:

pipe(
    findSchema(schema.info),
    TE.fromTaskOption,
    TE.swap
)

but TE.fromTaskOption doesn't return the TaskEither I expect. I also tried TE.fromTaskOptionK but it didn't work either, it returns a weird shaped function instead.

My use case is that in case the TaskOption is none I try saving (this means that the schema is not registered yet) otherwise I return an error (the schema is already registered). How can I solve this?

1 Answers

TE.fromTaskOption requires a value to put into Left, when the TO is None

Try this:

pipe(
    findSchema(schema.info),
    TE.fromTaskOption(() => "No schema"),
    TE.swap
)
Related