Skip to content

Latest commit

 

History

History
62 lines (52 loc) · 1.58 KB

File metadata and controls

62 lines (52 loc) · 1.58 KB

Home > types-kit > DeepPick

DeepPick type

Get the deep value path from T.

Signature:

export type DeepPick<T, K extends DeepKeys<T>> = {
  [P in keyof T as P extends OtherToString<K>
    ? P
    : K extends `${infer Head}.${string}`
    ? P extends Head
      ? P
      : never
    : never]: P extends OtherToString<K> // merge
    ? T[P]
    : [Exclude<K, Keys<T>>] extends [`${infer Head}.${infer Tail}`]
    ? P extends Head
      ? T[P] extends infer V
        ? V extends V
          ? IsObject<V> extends true
            ? DeepPick<
                V,
                Extract<
                  Tail extends Tail
                    ? `${P}.${Tail}` extends K
                      ? Tail
                      : never
                    : never,
                  DeepKeys<V>
                >
              >
            : V
          : never
        : never
      : never
    : never
}

References: DeepKeys, OtherToString, Keys, IsObject, DeepPick

Example

 interface Props {
      a?: {
        c: boolean
        d: () => void
        e: number
      }
      b: string
    }

 // Expect: { a?: { c: boolean; d: () => void } }
 type PropValues = DeepPick<Props, 'a.c' | 'a.d'>