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 typeO
and a default keyK
of typekeyof O
.K extends string
checks ifK
is a string literal, which means it is a valid key ofO
.O[K] extends Record<string, unknown>
checks if the property at keyK
is a nested object."${K}.${ObjectPaths<O[K]>}"
recursively combines the current keyK
with the paths of the nested object (if any).: K
returns the current keyK
if it is not a nested object.: never
returnsnever
ifK
is not a string literal (for example, ifO
is an empty object).
Note: Record<string, unknown>
is used to represent any object as long as it has string keys.