For my current project, I am tasked with defining a TypeScript type known as CardSize. This type has several possible variations, including static values, responsive (breakpoint-specific) values, and combinations of both separated by white space.
The singular values for CardSize are:
type CardSize =
'compact' |
'normal' |
'compact@small' |
'compact@medium' |
'compact@large' |
'normal@small' |
'normal@medium' |
'normal@large';
My ultimate goal is to create a CardSize type that allows for any combination of these values, without repetition and regardless of the order in which they appear. The use of Template Literal Types seems like the first step towards achieving this:
type CardSize = Size | `${Size}@${Breakpoint}`;
I have attempted to explore Permutations in order to generate all possible combinations of values but have faced some challenges in implementing it effectively.
In addition, I would like to impose two constraints on the CardSize type:
1. Restricting each string to contain only one specific breakpoint-value at a time (e.g. avoiding having both 'compact@small' and 'normal@small' in the same string)
2. Considering different sequences of values as equivalent, such as 'compact@small @normal@large' being treated the same as 'normal@large compact@small'
If anyone has insights on how to achieve this type of permutation or ensure type safety for CardSize without resorting to | string as a fallback, it would be greatly appreciated!