Consider the following:
type X = { label: 'Xlabel', X_id: 12 };
type Y = { label: 'Ylabel', Y_id: 24 };
type Z = { label: 'Zlabel', Z_id: 36 };
type CharSet = X | Y | Z;
I am looking for
type CharSetByLabel = Map<CharSet>; /* { Xlabel: X; Ylabel: Y; Zlabel: Z; } */
.
I attempted this approach and it worked well for a single type, but struggled with unions as I could not narrow down T
to each individual item (it resulted in
{ Xlabel: CharSet; Ylabel: CharSet; Zlabel: CharSet; }
).
type $withLabel = { label: string; }
type Map<T extends $withLabel> = {
[P in T['label']]: T
};
I also tried this method, however, it was unable to infer U
to a usable value.
type Map<T extends $withLabel> = {
[P in T['label']]: P extends T['label'] ? infer U : never;
};