For example a string that can only be two characters long, which could be used for an ISO country code.
I have used Google and looked through the documentation but cannot find the answer.
There isn't a way to specify a string length as a type.
If you have a finite list of country codes, you could write a string literal type:
type CountryCode = 'US' | 'CN' | 'CA' | ...(lots of others);
You can achieve this using a type constructor and a phantom type which are some interesting techniques to learn about. You can see my answer to a similar question here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With