0

Is it possible to define in an interface that a variable can only equal to a string from an array of strings, when an array is quite large and using union types don't seem to be feasible?

I have a list of country codes like ['US','GB','CY', 'PL'] and so on, about 200 in total.

Is it possible to do it something like

interface ICountryInfo {
    countryCode: CountryCodes;
}

Where CountryCodes is an array of codes?

Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
  • yes. It is called Enum and you can find information in this post http://stackoverflow.com/questions/15490560/create-an-enum-with-string-values-in-typescript – iberbeu Jul 26 '16 at 06:56

1 Answers1

0

You can use enume type to defines CountryCodes.

enum CountryCodes{
    US,
    GB,
    CY,
    PL
}

interface ICountryInfo {
    countryCode: CountryCodes;
}
matiii
  • 316
  • 4
  • 17