简体   繁体   中英

A union-like type with a large amount of items in TypeScript?

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?

You can use enume type to defines CountryCodes.

enum CountryCodes{
    US,
    GB,
    CY,
    PL
}

interface ICountryInfo {
    countryCode: CountryCodes;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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