35191-medium-trace

Back

// your answers 1
type Trace<T extends any[][], K extends unknown[] = []> = T extends [infer F extends unknown[], ...infer R extends any[][]]
  ? Trace<R, [...K, F[K['length']]]>
  : K[number]

// answer 2
type Trace<T extends any[][]> = {[P in keyof T]: T[P][P & keyof T[P]]}[number]

Solution by BruceYuj #35404

Feel happy about this solution 😊

type Trace<T extends any[][], All extends any[] = []> = T extends [
  ...infer R,
  infer L,
]
  ? L extends any[]
    ? R extends any[][]
      ? Trace<R, [...All, L[R["length"]]]>
      : "R is not any[][]"
    : "L is not any[]"
  : All[number];

Solution by StevenJPx2 #35311

type Trace<T extends unknown[][], C extends 1[] = [], R = never> = C['length'] extends T['length']
  ? R
  : Trace<T, [...C, 1], R | T[C['length']][C['length']]>;

Solution by alexandroppolus #35293

type Trace<T extends any[][]> = {[P in keyof T]: T[P][P & keyof T[P]]}[number]

Playground

Solution by teamchong #35247