I’m trying to define a type for a Theme definition object, which basically follows this structure:
const themes = {
default: {
dark: THEME_VALUES_DARK,
light: THEME_VALUES_LIGHT,
},
other_dark_theme_name: {
dark: OTHER_THEME_VALUES_DARK,
},
yet_another_light_theme_name: {
light: YET_ANOTHER_THEME_VALUES_LIGHT,
},
};
As you can see, not every theme supports both light and dark modes. There’s two things I’d ideally like to do here:
- Create an object defining the structure of which themes support which colour modes (to make it easy to iterate over all the available themes and colour modes). I’m thinking something like:
const THEME_SUPPORTED_MODES = {
default: ['light', 'dark'],
other_dark_theme_name: ['dark'],
yet_another_light_theme_name: ['light'],
};
- A type which derives the types of values available in
THEME_SUPPORTED_MODESor (if it’s more practical) to apply that type directly to thethemesobject above. I want to ensure that accessing members ofthemesis always done in a typesafe way, so that e.g.const values = themes.other_dark_theme_name.darkis typesafe, butconst values = themes.other_dark_theme_name.lightis not.
Can I achieve this with TypeScript, and if so how?