I have the following:
export const ObjC = Codec.struct({
name: Codec.string,
value: Codec.number,
})
export type ObjType = Codec.TypeOf<typeof ObjC>
I want a function for decoding this object and returning an Error (not DecoderError). Similar to:
import { fold } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
const decodeObj = async (str: string): Promise<Either<Error, ObjType>> => {
return pipe(
ObjC.decode(str),
fold(err => toError(err), m => m), // This doesn't do want I want
)
}
How can I return the right types for this function in an idiomatic way?
Just going off of the return type you've listed, it sounds to me like you want to perform a transformation to the left value when the thing is
Left
and leave the code unchanged when the value isRight
. In that case themapLeft
helper function is exactly what you're looking for.This can be achieved by saying:
However, I have some questions. For one the code as written will never succeed to parse because the input string will never match an object. I'm guessing there are other piece of logic you've omitted, but as it stands the code looks a bit wrong.