Use typescript to restrict string literal type

27 Views Asked by At

I would like to use typescript to enforce a "t-shirt" naming convention.

Can I for example create a typescript type TShirt which would work something like the following:

const name: TShirt = 'xs' // ok
const name: TShirt = 'xxxxxxxxxxs' // ok
const name: TShirt = 'xxxxxxxxxxxxl' // ok
const name: TShirt = 'xsz' // gives error

There are an infinite number of allowed types:

... xxxs, xxs, xs, s, m, l, xl, xxl, xxxl ...
0

There are 0 best solutions below