I am using React.CSSProperties for creating css variables so I can create like
let css: React.CSSProperties = {
position: "relative",
display: "inline-block",
background: "red"
}
I am trying to create a theme which will have css variables at the root level or nested inside modifiers like size(sm,md,lg) or variant(light,dark) or other as below
let theme: MyTheme = {
background: "red", // css
color: "white", // css
button: { // custom: component name Level1
background: "red", // css
color: "white", // css
light: { // custom: modifier variant Level2
background: "red", // css
color: "white", // css
hollow: { // custom: modifier Level3
borderColor: "red",
color: "red",
background: "transparent",
}
}
dark: {
background: "red",
color: "white",
hollow: {
borderColor: "black",
color: "black",
background: "transparent",
modfierLevel4: {
some: "css"
modfierLevel5: {
some: "css"
modfierLevel6: {
some: "css"
}
}
}
}
}
}
}
Basically I am looking for recursive object type in typescript like below, but ending up in circular reference?
type Modifiers = 'button' | 'dark' | 'light' | 'otherModifiers'
interface Theme extends React.CSSProperties {
[k in Modifiers]?: Theme;
}
I am able to find the answer (below) close to it.
type Theme = React.CSSProperties & { [keys in Modifiers]?: Theme }
But facing some issue. If modifier name is given invalid like "btn" instead of "button"
- error is thrown properly at level 1 (as expected)
- but no error is thrown from level 2 and above. (but expected to throw error)
How should I create types for the above?