ts定义对象中对象类型

定义

type IPlanTagProps = {
    
    
  content: string;
  bg?: string;
  color?: string;
};

type tagsProps = {
    
    
  [propName: string]: IPlanTagProps; // 关键
}

const tagsMap: tagsProps = {
    
    
  'a': {
    
     content: 'aa', bg: '#E9EDFF', color: '#264AFF' },
  'b': {
    
     content: 'bb', bg: '#FFF7E5', color: '#FFAF00' },
  'c': {
    
     content: 'cc', bg: '#FFECE5', color: '#FF3D00' },
};

使用

<PlanTag {
    
    ...tagsMap[_text]} />

猜你喜欢

转载自blog.csdn.net/qq_40657321/article/details/119803676