This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
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 typeOand a default keyKof typekeyof O.K extends stringchecks ifKis a string literal, which means it is a valid key ofO.O[K] extends Record<string, unknown>checks if the property at keyKis a nested object."${K}.${ObjectPaths<O[K]>}"recursively combines the current keyKwith the paths of the nested object (if any).: Kreturns the current keyKif it is not a nested object.: neverreturnsneverifKis not a string literal (for example, ifOis an empty object).
Note: Record<string, unknown> is used to represent any object as long as it has string keys.