This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for TypeScript puzzle on BFE.dev
57. implement ObjectPaths<O>

type ObjectPaths<O, K = keyof O> =
  K extends string
  ? O[K] extends Record<string, unknown>
    ? `${K}.${ObjectPaths<O[K]>}`
    : K
  : never;

Explanation:

  • ObjectPaths<O, K = keyof O> takes in an object type O and a default key K of type keyof O.
  • K extends string checks if K is a string literal, which means it is a valid key of O.
  • O[K] extends Record<string, unknown> checks if the property at key K is a nested object.
  • "${K}.${ObjectPaths<O[K]>}" recursively combines the current key K with the paths of the nested object (if any).
  • : K returns the current key K if it is not a nested object.
  • : never returns never if K is not a string literal (for example, if O is an empty object).

Note: Record<string, unknown> is used to represent any object as long as it has string keys.