Typescript: Transform A Union Of Objects To A Plain Union
How to transform a union of objects to a plain union with object's values. An example: type Foo = {}; type Bar = {}; type Baz = {}; type Qux = {}; type Input = { foo: Foo, bar
Solution 1:
You want to take something like a ValueOf<T> type (which is just T[keyof T]) and distribute it across the union members of T so that if T is, for example, A | B | C, then Transform<T> is A[keyof A] | B[keyof B] | C[keyof C]. You can use a distributive conditional type to get that behavior:
type Transform<T> = T extends any ? T[keyof T] : never;
Let's test it:
type Output = Transform<Input>
// type Output = Foo | Bar | Baz | Qux
Looks good.
Post a Comment for "Typescript: Transform A Union Of Objects To A Plain Union"