Understanding the Either.filterOrLeft function and its limitations

Hi, maybe I am doing something wrong with the filter functions. Let's take for instance the Either.filterOrLeft function:
export const : filterOrLeft function<A, E2>(
    predicate: Predicate<NoInfer<A>>,
    orLeftWith: (a: NoInfer<A>) => E2
  )=> <E>(self: Either<E, A>) : Either<E2 | E, A>

Sometimes I need to do some complex calculation in the predicate part to determine if the Either is valid or not. Issue is that I would like to retrieve part of this calculation in the orLeftWith function because I can then provide a more detailed error message. So ideally, I would like something like:
export const : filterOrLeft function<A, E2,I>(
    predicate: (a:NoInfer<A>) => [boolean,I]
    orLeftWith: (a: NoInfer<A>, info:I) => E2
  )=> <E>(self: Either<E, A>) : Either<E2 | E, A>

For the moment, what I do is perform the calculation before the filter and put the result into a tuple, then perform the filter on that tuple, and finally map the tuple back to the original value. For example:
pipe(
    Either.right(1),
    Either.map((n) => Tuple.make(n, n * 2)),
    Either.filterOrLeft(
        ([initialValue, extraCalcForFiltering]) => initialValue <= extraCalcForFiltering,
        ([initialValue, extraCalcForFiltering]) =>
            new Error(`Value too large. Expected: <${extraCalcForFiltering}, actual:${initialValue}`)
    ),
    Either.map(([initialValue]) =>initialValue)
);

Is this the right way to proceed?
Was this page helpful?