All files / types DotPath.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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                                                                           
// ref: https://github.com/ahejlsberg/tsconf2020-demos/blob/master/template/main.ts#L108
type SubKeys<T, K extends string | number> = K extends keyof T
  ? `${K}.${DotPath<T[K]>}`
  : never
 
export type DotPath<T> = object extends T
  ? string
  : T extends any[]
  ? '0' | SubKeys<T, 0>
  : T extends readonly any[]
  ? Extract<keyof T, `${number}`> | SubKeys<T, Extract<keyof T, `${number}`>>
  : T extends object
  ? Extract<keyof T, string> | SubKeys<T, Extract<keyof T, string>>
  : never
 
export type DotPathValue<T, Path extends string> = Path extends '0'
  ? // @ts-ignore
    T[0]
  : Path extends keyof T
  ? T[Path]
  : Path extends `${infer K}.${infer R}`
  ? K extends '0'
    ? DotPathValue<
        // @ts-ignore
        T[0],
        R
      >
    : K extends keyof T
    ? DotPathValue<T[K], R>
    : unknown
  : unknown
 
export type DotPathWithRoot<T> = DotPath<T> | '.'
 
export type DotPathWithRootValue<T, Path extends string> = Path extends '.'
  ? T
  : DotPathValue<T, Path>