I have a type like this:
{
a: number,
b: number,
c: undefined
}
And I want to map it to a type like this:
{
a: number,
b: number
}
i.e. I want to omit all keys where the type of the value is undefined by definition. The type comes from a generic so I obviously cannot do it manually.
I know I can map a type to another type with a mapper, so I tried mapping undefined to never:
type TryOmitUndefined<K> = {
[k in keyof K]: K[k] extends undefined ? never : K[k]
}
But this doesn't work at all.
Is it possible in current TypeScript to omit keys from a type based on their value?
Bonus question
Can I map this
{
a: number,
b: number | undefined,
c: undefined
}
to this?
{
a: number,
b?: number
}