Is there any function in fp-ts capable of lifting all values of an object to an option and keep the shape of the type? The cherry on top will be to be able to not transform any value that is already option. Something like this:
({ a: string, b: Option<number> }) => { a: Option<string>, b: Option<number>}
The short answer to this question is "no", as far as I can tell,
fp-tsprovides no helper to achieve exactly what you're looking for. The idea of "keep[ing] the shape of the type" is something thatfp-tsrefers to as a "struct". There is astruct.tssection of the library but it is fairly limited at the time of writing.If I understand your question correctly, you are looking for a function that operates on a struct and converts the type at each key roughly according to the following types:
And
optionifydoes not exist in the library. We could try and make one though. The main obstacle is detecting if something is an option.fp-tsdoes not actually declare anOptionclass so we cannot useinstanceofto check if something coming in is really anOption, but we can make a best guess by breaking some layers of abstraction and looking for an_tagfield that says'None'or'Some'as per the current implementation.So a possible implementation might look like:
One last note, if you don't have a struct but instead have say a
Record, then you could usefp-ts/lib/Record'smapfunction to applyflatOptand avoid at least the gotchas described withoptionify.