I have a TypeScript type which has a bunch of strings and it's defined like this:
type Regions = "" | "eu-west-1" | "eu-east-1" | "us-west-1" | "us-east-1" | "ap-southeast-1" | "ap-east-1"
I then later on in my code get the region from the URL via query-string parameters. Let's say that the retrieved region from URL is a variable named region
So i'm doing some validation in my code and I want make sure the region is of the type Regions.
So in order to do the validation my code says:
if (region in Regions) { doStuff();}
Which would work if Region was a variable with all those regions.
However since Regions is a type, it's throwing an error saying:
'Regions' only refers to a type, but is being used as a value here
Which I understand why, but how can I achieve what i want to do using or extending the type Regions
I really don't want to create another object or array of strings, I just want use my existing type to do this validation.
I did create an enum like this:
enum RegionsList {
'eu-west-1',
'eu-east-1',
'us-west-1',
'us-east-1',
'ap-southeast-1',
'ap-east-1'
}
and then did region in RegionsList but this is kinda duplicating my Regions type already, and there has to be a better solution that combines the 2. Any help/suggestions is appreciated.