As TypeScript 4.1 was released, many developers have been exploring ways to strictly type strings with predetermined patterns. I recently found a good solution for date strings, but now I'm tackling the challenge of Hex color codes.
The simple approach of defining HEX = 0 | 1 | ... | "E" | "F" and then declaring
type HEX_CODE = `#${HEX}${HEX}${HEX}${HEX}${HEX}${HEX}`;
creates a type that is too complex due to the union of too many types.
So, I thought about requiring the "#" symbol in front of the hex code like this:
type HEX_CODE = `#${arbitraryString}`;
However, I'm struggling to implement this. If anyone knows how to make it work or can suggest another compromise on the type, please share your insights.