ts 保留或排除,必填或选填

1.保留或排除

interface AddFieldTemp {
  id: string;
  name: string;
  // other fields..
  createTime: string; 
  updateTime: string;
}

type SubsetType = Pick<AddFieldTemp, 
  'name' | 'originalName' | 'enterpriseId' | // 要保留的字段
> & Omit<AddFieldTemp, 
  'id' | 'createTime' | 'updateTime'  // 要排除的字段
>

 

2.必填或选填

interface AddFieldTemp {
  id: string; 
  name?: string;
  age?: number;
  address: string;
}

type SubRequired = Required<
  Partial<Omit<AddFieldTemp, 'name' | 'age'>>
>

// 等价于
type SubRequired = {
  id: string;
  address: string; 
}

 

posted @ 2023-12-11 14:01  最好的年纪  阅读(248)  评论(0编辑  收藏  举报