Is it possible from this type:
type Input = {
foo: "a" | "b";
bar: "c" | "d";
};
to optain this type:
type Output =
{ key: "foo"; value: "a" | "b"; } |
{ key: "bar"; value: "c" | "d"; };
?
Thank you!
CodePudding user response:
Yeah, you can do that with mapped types.
type TransformInput<T> = {
[P in keyof T]: { key: P; value: T[P] };
}[keyof T];
type Output = TransformInput<Input>
Output
will evaluate to
type Output = {
key: "foo";
value: "a" | "b";
} | {
key: "bar";
value: "c" | "d";
}