How do I check if 'key' is correctly populated with 'known' values in JSON file

后端 未结 3 1959
Happy的楠姐
Happy的楠姐 2021-01-23 01:49

I am trying to check if a particular key is assigned with only a set of values. This values are listed as an enum in Typescript.

P

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-23 02:11

    Do something like this:

    function isValidRegion(candidate: any): candidate is Regions {
          return Object.keys(Regions).some(region => region === candidate)
    }
    

提交回复
热议问题