简体   繁体   English

Typescript:正则表达式作为枚举值

[英]Typescript: Regex as enum value

I am making API calls from an API in typescript and I would like to clearify how the response looks like using an interface.我正在从 typescript 中的 API 进行 API 调用,我想澄清一下使用接口时响应的样子。 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.其中 x 可以是任何正数 integer。 And this x is the problem...而这个 x 就是问题所在……

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?如果不可能,我只需将枚举更改为字符串类型,但您还有其他想法如何做到这一点吗?

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.感谢 Christian 提出不使用枚举的想法。 I created my own type which takes either one of the first several "enum strings" or a RegEx.我创建了自己的类型,它采用前几个“枚举字符串”中的一个或 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM