根据其他答案,我有一个 colors
用于 针迹 ,但对于组件中用作道具的每种颜色,我都执行以下操作。
const colors = {
white: '#ffffff',
black: '#000000',
transparent: 'transparent',
text: '$black',
background: '$white',
indigo: '#0757D6',
purple: '#7928ca',
success: '#17c964',
warning: '#f5a623',
yellow2: '#F2C94C',
error: '#f21361',
primary: '#518AE3',
secondary: '$purple',
icon: '#BBBBBB',
primaryOpacity: '#DEEBFF',
secondaryOpacity: '#DBC8F0',
errorOpacity: '#FFC3D8',
subText: '#707070',
gray1: '#333333',
bgCard: '#F9F9FA',
bgText: 'rgba(234, 234, 234, 0.7)',
};
要实现 ColorType
自动化,请尝试以下操作:
type addPrefixToObject<TObject extends object, TPrefix extends string> = `${TPrefix}${keyof TObject}`
export type ColorType = addPrefixToObject<typeof colors, '$'>;
这将返回一个类型 "$black" | "$white" | "$text"
(等等)。即它返回每个带有前缀的颜色键 $
.