I have several interfaces such as Foo
, Bar
, Baz
, and I want to create a union of their mapped types where the mapping is consistent (e.g., using Pick
).
interface Foo {
a: 'FooA';
b: 'FooB';
}
interface Bar {
a: 'BarA';
b: 'BarB';
}
interface Baz {
a: 'BazA';
b: 'BazB';
}
I can achieve this manually:
type A = Pick<Foo, 'a'> | Pick<Bar, 'a'> | Pick<Baz, 'a'>;
type B = Pick<Foo, 'b'> | Pick<Bar, 'b'> | Pick<Baz, 'b'>;
However, I would like to avoid repeating myself. The following code does not accomplish this as it unions the properties of types and then maps them afterwards:
type Union = Foo | Bar | Baz;
type A = Pick<Union, 'a'>; // creates { a: 'FooA' | 'BarA' | 'BazA' } but I need { a: 'FooA' } | { a: 'BarA' } | { a: 'BazA' }
type B = Pick<Union, 'b'>;
Is there another approach to achieve this?