I am creating a theme handling function.
The function that has a desiredColor
parameter. I want passed in argument to only be one of the keys from the interface provided as the type for the underlying colors.
Is there a way to restrict the desiredColor
to only be one of the key values of the underlying interface?
(is there a way to do this without also having to maintain an adjacent enum?)
const handleThemeColors = (mode: ThemeMode): any => {
const colorsHandler = (desiredColor: Array<keyof PaletteColors>) =>
// goal ^ desiredColor can be either: blueDark, blueMain, or blueLight
{
switch (mode) {
case 'light':
return colorsLight[desiredColor];
default:
return colorsDark;
}
};
...
}
interface PaletteColors {
blueDark: string;
blueMain: string;
blueLight: string;
}
const colorsLight: PaletteColors = {
blueDark: 'rgba(2, 189, 185,1)',
blueMain: 'rgba(0, 203, 198,1)',
blueLight: 'rgba(147, 231, 229,1)'
}
CodePudding user response:
Surely, it's as simple as
const colorsHandler = (desiredColor: keyof PaletteColors) =>