I'm attempting to transform a Tuple union in TypeScript to an object, without losing any types.
Here is an example of how it would work:
type Tuples = ["foo", string] | ["bar", boolean] | ["baz", null];
/*
ideally the type would be:
{
foo: string;
bar: boolean;
baz: null;
}
*/
type AsObject = DoSomething<Tuples>;
A simple solution to the above would be:
type TupleToObject<T extends [string, any]> = { [key in T[0]]: T[1] };
/*
type is:
{
foo: string | boolean | null;
bar: string | boolean | null;
baz: string | boolean | null;
}
*/
type TypesLost = TupleToObject<Tuples>;
However we lose some type information as all values are pulled together in one union type.
I'm looking for a solution using generics that does not lose this type information, and would appreciate any deeper insight into mapping generic tuples in TypeScript.
Extract
was exactly what I was blanking on. – Heinrick