Is there a way to extract a particular property of a union type and create a type from that?
Let's say we have this:
type Actions =
| {
type: "ADD_COLUMN";
newColumnIndex: number;
column: SelectorColumnData;
}
| {
type: "ITEM_CHECKED";
columnIndex: number;
item: SelectorItem;
};
Is there some magic generic stuff we can use to produce something like:
type ActionTypes = GetTypes<Actions>
// ActionTypes = "ADD_COLUMN" | "ITEM_CHECKED"
Actions["type"]- However, if one of the constituents ofActionsdoes not have atypeproperty, it'll be a little more complicated. You would have to extract all constituents first:Extract<Actions, { type: any }>["type"].