Search code examples
typescriptfp-ts

Converting an fp-ts Either to an Effect Either


I have been using fp-ts for some time. Lately, I have been thinking about migrating some parts of my codebase to using Effect instead. I'm looking looking for bridges between the two. One of the first obstacles I have run into, is that the Either data structures used by two libraries differ slightly from each other. What would be a good way of converting between the two?

import * as E from '@effect/data/Either'
import * as F from 'fp-ts/Either'

const fe: F.Either<string, number> = F.right(123)
// @ts-expect-error not assignable
const ee: E.Either<string, number> = fe

Solution

  • Sounds like you would need to destructure the fp-ts Either and reconstruct the Effect Either.

    Something like this:

    pipe(
      fe,
      F.matchW(E.left, E.right)
    )
    

    This would convert an "fp-ts Left" -> "Effect Left" and an "fp-ts Right" -> "Effect Right"