TypeScript:与 Pick<...> 相同,但具有多个字段

ama*_*iny 10 typescript

我使用Pick,但是我怎么能写一个可以选择多个字段的通用 PickMulti 呢?

interface MyInterface {
  a: number,
  b: number,
  c: number
}

// this works, but I would like a generic one in number of fields
type AB = Pick<Pick<MyInterface, 'a'>, 'b'>;

// Something like this:
type PickMulti = /* how to write this?*/;
type AB = PickMulti<MyInterface, ['a', 'b']>
Run Code Online (Sandbox Code Playgroud)

Tit*_*mir 18

Pick 已经可以处理多个字段,您只需要将它们作为联合提供,而不是元组/数组类型:

interface MyInterface {
  a: number,
  b: number,
  c: number
}

type AB = Pick<MyInterface, 'a' | 'b'>;

Run Code Online (Sandbox Code Playgroud)

游乐场链接