Say I have some types:
type A = ...;
type B = ...;
type C = ...;
type MyTypes = A | B | C;
And then I have a generic type transformer:
type Transformed<T extends MyTypes> = ...;
Then I would like to automatically export all transformed types, i.e. autogenerate:
type ATrans = Transformed<A>;
type BTrans = Transformed<B>;
type CTrans = Transformed<C>;
Without having to type that every time I add a new type to MyTypes.
Is this possible?
Thanks,
CodePudding user response:
You can use distributive conditional types:
type A = { tag: 'a' };
type B = { tag: 'B' };
type C = { tag: 'C' };
type MyTypes = A | B | C;
type Transformation<T> = { transformed: T }
type Transformed<T extends MyTypes> = T extends any ? Transformation<T> : never
// Transformation<A> | Transformation<B> | Transformation<C>
type Result = Transformed<MyTypes>
After this line T extends any
union gets distributed