I want to write some basic types for my tailwind config since none of the libraries that exist work how I would like them to (and I don't have time to make my own right now). I would like to take the colours from my tailwind config and create a 1 level deep object that has all of the keys of the nested objects joined together with - to form the equivalent tailwind classes. So, for example, with the given object:
white: "#fff",
black: "#000",
neutral: {
100: "#e6e6e6",
200: "#cccccc",
300: "#b3b3b3",
400: "#999999",
500: "#808080",
600: "#666666",
700: "#4c4c4c",
800: "#333333",
900: "#191919",
},
I would like an type to be generated that is:
{
white: "#fff",
"black": "#000",
"neutral-100": "#e6e6e6",
"neutral-200": "#cccccc",
"neutral-300": "#b3b3b3",
"neutral-400": "#999999",
"neutral-500": "#808080",
"neutral-600": "#666666",
"neutral-700": "#4c4c4c",
"neutral-800": "#333333",
"neutral-900": "#191919",
}
So far I've got this:
type FlattenColors<T, Current extends string = ""> = {
[K in keyof T]: T[K] extends string
? T[K]
: Current extends ""
? FlattenColors<T[K], `${K}`>
: FlattenColors<T[K], `${Current}-${K}`>
};
but obviously it isn't very close and I'm not sure how I should approach it