Typescript: Regex as enum value

Viewed 1580

I am making API calls from an API in typescript and I would like to clearify how the response looks like using an interface. One of its values is an string which can only have a (defined) number of values. In my opinion that is the what enums are for, right?

These are the possible values:

"Normal Goal", "Own Goal", "Penalty", "Missed Penalty", 
"Yellow Card", "Second Yellow Card", "Red Card",
"Substitution x"

where x can be any positive integer. And this x is the problem...

To implement it I tried using Regex. This is what it looks like:

enum Event {
  "Normal Goal", "Own Goal", "Penalty", "Missed Penalty", 
  "Yellow Card", "Second Yellow Card", "Red Card",
  "Substitution [1-9]\d*$"
}

But it does not seem to work. If it isn't possible I'll just have to change the enum to type string but do you have any other idea how one could do this?

2 Answers

I would suggest using one regular expression for all values instead of an enum and check the value in runtime :

const event : RegExp = /(?:^|(?<= ))(Normal Goal|Own Goal|Penalty|Missed Penalty|Yellow Card|Second Yellow Card|Red Card|Substitution [1-9]\d*$)(?:(?= )|$)/

If you absolutely want it as a type you need to expand the regex to literals in your type:

type MatchEvent = 
        "Normal Goal"|
        "Own Goal"|
        "Penalty"|
        "Missed Penalty"|
        "Yellow Card"|
        "Second Yellow Card"|
        "Red Card"|
        "Substitution 1"|
        "Substitution 2"|
        "Substitution 3"|
        "Substitution 4"|
        "Substitution 5"|
        "Substitution 6"|
        "Substitution 7"|
        "Substitution 8"|
        "Substitution 9"|
        "Substitution 10"|
        "Substitution 11"|
        "Substitution 12"|
        "Substitution 13"|
        "Substitution 14"|
        "Substitution 15"|
        "Substitution 16"|
        "Substitution 17"|
        "Substitution 18"|
        "Substitution 19"|
        "Substitution 20"|
        "Substitution 21"|
        "Substitution 22"|
        "Substitution 23"|
        "Substitution 24"|
        "Substitution 25"|
        "Substitution 26"|
        "Substitution 27"|
        "Substitution 28"|
        "Substitution 29"|
        "Substitution 30"|
        "Substitution 31"|
        "Substitution 32"|
        "Substitution 33"|
        "Substitution 34"|
        "Substitution 35"|
        "Substitution 36"|
        "Substitution 37"|
        "Substitution 38"|
        "Substitution 39"|
        "Substitution 40"|
        "Substitution 41"|
        "Substitution 42"|
        "Substitution 43"|
        "Substitution 44"|
        "Substitution 45"|
        "Substitution 46"|
        "Substitution 47"|
        "Substitution 48"|
        "Substitution 49"|
        "Substitution 50"|
        "Substitution 51"|
        "Substitution 52"|
        "Substitution 53"|
        "Substitution 54"|
        "Substitution 55"|
        "Substitution 56"|
        "Substitution 57"|
        "Substitution 58"|
        "Substitution 59"|
        "Substitution 60"|
        "Substitution 61"|
        "Substitution 62"|
        "Substitution 63"|
        "Substitution 64"|
        "Substitution 65"|
        "Substitution 66"|
        "Substitution 67"|
        "Substitution 68"|
        "Substitution 69"|
        "Substitution 70"|
        "Substitution 71"|
        "Substitution 72"|
        "Substitution 73"|
        "Substitution 74"|
        "Substitution 75"|
        "Substitution 76"|
        "Substitution 77"|
        "Substitution 78"|
        "Substitution 79"|
        "Substitution 80"|
        "Substitution 81"|
        "Substitution 82"|
        "Substitution 83"|
        "Substitution 84"|
        "Substitution 85"|
        "Substitution 86"|
        "Substitution 87"|
        "Substitution 88"|
        "Substitution 89"|
        "Substitution 90"|
        "Substitution 91"|
        "Substitution 92"|
        "Substitution 93"|
        "Substitution 94"|
        "Substitution 95"|
        "Substitution 96"|
        "Substitution 97"|
        "Substitution 98"|
        "Substitution 99";

Thanks to Christian for coming up with the idea of not using an enum. I created my own type which takes either one of the first several "enum strings" or a RegEx.

My code looks like this now:

type Event = ("Normal Goal"|"Own Goal"|"Penalty"|"Missed Penalty"|"Yellow Card"|"Second Yellow Card"|"Red Card"|"Substitution [1-9]\d*$)(?:(?= )|$");

interface I {
  test: Event
}
Related