Tyler Gould
Tyler Gould

Reputation: 82

Making a Flow union type of strings from properties in a union type of objects

I have a union type of object types like:

type Action = { type: 'A' } | { type: 'B' } | { type: 'C' } // and so on

I want to make a union type of their type properties, like:

type ActionType = 'A' | 'B' | 'C' // and so on

Is there a way to do this programmatically in Flow (i.e. so I don't have to manually list out all the string literals to make ActionType)? I couldn't figure it out using $ObjMap, $PropertyType, $Call, or any combination thereof.

Upvotes: 1

Views: 191

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161457

$PropertyType and $ElementType both work here.

type ActionType = $PropertyType<Action, "type">;
// OR
type ActionType = $ElementType<Action, "type">;

Upvotes: 2

Related Questions