fp-ts

What is the difference between Either<string, A> and Either<Error, A>


I'm new to fp-ts, I saw some people use Either<string, A> and others use Either<Error, A> in different articles. I want to know the difference between the two, and how should I choose which one to use? Thanks!


Solution

  • Either is a tool to help you return one of two possible values from a function. Traditionally it is used to message to the call site when a process can fail. Failures are returned in the Left variant, whereas successful values are returned in the Right variant.

    So with Either<string, A> the function is promising to return the Right<A> value when it succeeds or a Left<string> value when it fails. In that case, the failure is most likely a string containing a message describing the failure. Either<Error, A> is similar, except that the author of that function is using the existing Error class to contain information about the failure.

    It's up to the designer of the function's API to decide what value it makes sense to return on failure, but neither is necessarily wrong.

    Incidentally, the Left value being failure and the Right value being success is only convention. It's perfectly valid to have a function that returns Either<number, string> and return number in some situations and string in others.