I have an enumerated type called Country and another type that represents a subset of European countries. I want to handle values within this subset differently from others. Currently, I am using an if statement with multiple conditions, but it could get unwieldy as the number of values in the subset grows.
enum Country {
Canada,
Finland,
France,
Germany,
Japan,
Peru,
}
type EuropeanCountries = Country.Finland | Country.France | Country.Germany;
const NonEuropeanCountriesContinent: Record<
Exclude<Country, EuropeanCountries>,
string
> = {
[Country.Canada]: "America",
[Country.Japan]: "Asia",
[Country.Peru]: "America",
}
function getContinent(country: Country) {
// Instead of manually checking each value, is there a better way to determine if the country is in the subset?
if (country === Country.France || country === Country.Finland || country === Country.Germany) {
return "Europe";
}
return NonEuropeanCountriesContinent[country];
}
How can I efficiently verify whether the value of my country variable is among the specified European countries without having to individually inspect each one?