All files / utils pickAll.ts

100% Statements 1/1
100% Branches 0/0
100% Functions 1/1
100% Lines 1/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                            5x    
import { pick } from 'lodash-uni'
 
// https://github.com/sindresorhus/type-fest/issues/656
type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B
  ? 1
  : 2
  ? true
  : false
type TupleToUnion<ArrayType> = ArrayType extends readonly unknown[]
  ? ArrayType[number]
  : never
type TupleOf<Tuple extends ReadonlyArray<Values>, Values> = IsEqual<
  Values,
  TupleToUnion<Tuple>
> extends true
  ? Tuple
  : never
 
// 过于卡顿
// https://github.com/microsoft/TypeScript/issues/13298#issuecomment-692864087
// type TupleUnion<U extends string, R extends string[] = []> = {
//   [S in U]: Exclude<U, S> extends never
//     ? [...R, S]
//     : TupleUnion<Exclude<U, S>, [...R, S]>
// }[U] &
//   string[]
 
/**
 * 选中对象中的所有类型上可见的属性并返回。
 *
 * @param data 对象
 * @param keys 属性列表
 */
export function pickAll<
  T extends Record<string, any>,
  K extends keyof T,
  Tuple extends ReadonlyArray<K>,
>(data: T, keys: TupleOf<Tuple, K>): T {
  return pick(data, keys as any) as any
}