Looking for a solution to transform a discriminated union similar to this:
type Something = {
type: 'mode';
values: 'first' | 'second';
} | {
type: 'part';
values: 'upper' | 'lower';
};
into
{
mode: 'first' | 'second';
part: 'upper' | 'lower';
}
with the help of a generic type?
I've attempted something along these lines:
type MyUnion = {
type: string;
values: string;
};
type DiscUnionToObject<U extends MyUnion> = {
[V in U['type']]: U['values']
}
however, when calling
DiscUnionToObject<Something>
, I get
{
mode: 'first' | 'second' | 'upper' | 'lower';
part: 'first' | 'second' | 'upper' | 'lower';
}
I'm struggling to figure out how to make the generic type recognize that 'upper' | 'lower'
should not be included for Something
when type
is set to mode
.