我正在创建一个主题处理函数。
有desiredColor
参数的函数。我希望传入的参数只能是接口中作为底层颜色类型提供的键之一。
是否有办法限制desiredColor
仅为一个底层接口的键值?
(有没有一种方法可以做到这一点,而不必维护相邻的枚举?)
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)'
}
当然,就像
const colorsHandler = (desiredColor: keyof PaletteColors) =>