Typescript学习: Omit 与 Pick

typescript的2个常用工具
更新于: 2023-05-12 10:46:10

基本理解

  • Omit: 剔除/删掉
  • Pick: 挑选

Omit基本用法

type SuperbUser = {
  userId: number,
  macAddress: string,
  username: string,
  email: string,
  password: string,
  firstName: string,
  lastName: string,
  roles: ('Admin' | 'Editor' | 'Author')[]
};

type Subscriber = Omit<SuperbUser, 'roles'>;

Pick基本用法

interface SuperbUser {
  userId: number;
  macAddress: string;
  username: string;
  email: string;
  password: string;
  firstName: string;
  lastName: string;
  roles: ('Admin' | 'Editor' | 'Author')[]
};

type GuestUser = Pick<SuperbUser, 'userId' | 'macAddress' | 'username'>;

参考