Hello, I spent some time on a little function:
here is the situtation:
I have a object containing Option (from fp-ts but whatever);
const input = {
name: O.Some("Hello"),
age: O.None(),
gender: O.Some("M")
}
And i want to do 2 things, remove the key where value is "None", and map value where type if Some:
exemple output:
const output = {
name: "hello",
gender: "M"
I cannot make the type to match what i have in mind, everytime, the output is infered to uknown, or string, or something I don't want.
(runtime work, only typescript cause me troubles)
Here is my last attempt:
function excludeNoneFromObject<
OldObject extends Record<string, O.Option<V>>,
V extends string,
>(data: OldObject): Record<keyof OldObject, V> {
return Object.fromEntries(
Object.entries(data)
.filter(([_, value]) => O.isSome(value))
.map(([key, value]) => [key, O.toNullable(value)]),
) as Record<keyof OldObject, V>;
}
I feel it's too personal to post it on stackoverflow,
thanks in advance for your help