1

This question may seem similar to the one about converting strings to enums, but my situation is a bit more complicated. I have two different enums and another sum type over them:

enum InteractiveStep {
    Pick = 'PICK',
    Ban = 'BAN'
}

enum AutoStep {
    RandomPick = 'RANDOM_PICK',
    LastPick = 'LAST_PICK'
}

type ActionStep = InteractiveStep | AutoStep

How can I check whether an arbitrary string is contained in the ActionStep type, and safely convert it?

Max Yankov
  • 12,551
  • 12
  • 67
  • 135

1 Answers1

0

Using this discussion, I was able to come up with this:

enum InteractiveStep {
    Pick = 'PICK',
    Ban = 'BAN'
}

enum AutoStep {
    RandomPick = 'RANDOM_PICK',
    LastPick = 'LAST_PICK'
}

type ActionStep = InteractiveStep | AutoStep
const ActionStep = {...InteractiveStep, ...AutoStep}

const inputString: string = 'RANDOM_PICK'

const inputStep: ActionStep = ActionStep[inputString as keyof typeof ActionStep];
Max Yankov
  • 12,551
  • 12
  • 67
  • 135