这个问题似乎类似于the one about converting strings to enums,但我的情况要复杂一些。我在上面有两个不同的枚举和另一个和式:
enum InteractiveStep {
Pick = 'PICK',
Ban = 'BAN'
}
enum AutoStep {
RandomPick = 'RANDOM_PICK',
LastPick = 'LAST_PICK'
}
type ActionStep = InteractiveStep | AutoStep
如何检查ActionStep
类型中是否包含任意字符串,并安全地对其进行转换?
答案 0 :(得分:0)
使用this discussion,我可以提出以下建议:
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];